Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeguard fails on command object looking for __code__ #1927

Closed
staticdev opened this issue May 22, 2021 · 6 comments
Closed

typeguard fails on command object looking for __code__ #1927

staticdev opened this issue May 22, 2021 · 6 comments

Comments

@staticdev
Copy link

staticdev commented May 22, 2021

After updating from click 8.0.0 to 8.0.1 problems with mypy related to #1879 vanished. But still when I run dynamic type-checking with typeguard it is failing. I works perfectly on 7.1.2.

Steps to reproduce:
I run typeguard as a pytest plugin.

If you run this code with:

poetry install
nox -s typeguard

You will get the errors on here:

============================= test session starts ==============================
platform linux -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /home/runner/work/irpf-cei/irpf-cei
plugins: typeguard-2.12.0, pyfakefs-4.4.0, mock-3.6.1
collected 38 items / 1 error / 37 selected

==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_main.py ______________________
tests/test_main.py:6: in <module>
    from irpf_cei import __main__
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
.nox/typeguard-3-9/lib/python3.9/site-packages/typeguard/importhook.py:81: in exec_module
    return super().exec_module(module)
.nox/typeguard-3-9/lib/python3.9/site-packages/irpf_cei/__main__.py:61: in <module>
    def main() -> None:
.nox/typeguard-3-9/lib/python3.9/site-packages/typeguard/__init__.py:952: in typechecked
    if python_func.__code__ is not wrapper.__code__:
E   AttributeError: 'Command' object has no attribute '__code__'
=========================== short test summary info ============================
ERROR tests/test_main.py - AttributeError: 'Command' object has no attribute ...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.45s ===============================

Expected behavior: it runs all tests detecting correct types.

Environment:

  • Python version: 3.9.5
  • Click version: 8.0.1
@davidism
Copy link
Member

The command class doesn't have a code attribute though.

@epenet
Copy link

epenet commented May 25, 2021

I have a similar issue on https://github.com/hacf-fr/renault-api (see https://github.com/hacf-fr/renault-api/runs/2664117342), trying to update from 7.1.2 to 8.0.1
Except that for me, it's the Group not the Command raising the issue.

tests\cli\test_vehicle.py:21: in <module>
    from renault_api.cli import __main__
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
.venv\lib\site-packages\typeguard\importhook.py:81: in exec_module
    return super().exec_module(module)
src\renault_api\cli\__main__.py:21: in <module>
    from .charge import commands as charge_commands
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
.venv\lib\site-packages\typeguard\importhook.py:81: in exec_module
    return super().exec_module(module)
src\renault_api\cli\charge\commands.py:6: in <module>
    from . import schedule
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
.venv\lib\site-packages\typeguard\importhook.py:81: in exec_module
    return super().exec_module(module)
src\renault_api\cli\charge\schedule.py:33: in <module>
    def schedule() -> None:
.venv\lib\site-packages\typeguard\__init__.py:952: in typechecked
    if python_func.__code__ is not wrapper.__code__:
E   AttributeError: 'Group' object has no attribute '__code__'

@davidism
Copy link
Member

Seems to be a bug in typeguard, they're expecting something to have a __code__ attribute when it does not. Please report this to typeguard.

If you want to use git bisect to track down what change to Click caused this, that would help determine if the change was intentional and if Click should have a fix or if it should only be reported to typeguard.

@davidism davidism changed the title Dynamic type-checking still struggling with click 8 typeguard fails on command object looking for __code__ May 25, 2021
@epenet
Copy link

epenet commented May 26, 2021

@davidism maybe you could keep this open a little longer - until the root cause or a workaround is found.

git bisect tracked it down to 5a77d69

Specifically, reverting the changes on lines 1629 and 1641 makes the problem go away:

Before the commit: UserWarning: no type annotations present -- not typechecking click.core.<Group xxxxx>
After the commit: AttributeError: 'Group' object has no attribute '__code__'

@epenet
Copy link

epenet commented May 26, 2021

I think the issue is that the @click.group() decorator effectively replaces a function (xxx) with a class instance and an associated callback (<Group xxx>)
Is there a way to create a group without using the decorator? All the documentation points to the decorator.

@epenet
Copy link

epenet commented Jun 2, 2021

Typeguard fix has been merged, and will be in next release: agronholm/typeguard#190

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants