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

*args with forward ref not working with pytest #474

Open
2 tasks done
manulera opened this issue Jul 19, 2024 · 2 comments
Open
2 tasks done

*args with forward ref not working with pytest #474

manulera opened this issue Jul 19, 2024 · 2 comments
Labels

Comments

@manulera
Copy link

manulera commented Jul 19, 2024

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.3.0

Python version

3.11.7

What happened?

First of all, thanks for the great library, I think it's a great idea and I am hoping to implement it in a library that I maintain.

Below is a minimal module that causes the error:

example.py: the module file

from typing import List

class OtherClass:
    def __init__(self, *args: List["ForwardRef"], **kwargs: str) -> None:
        args[0][0].name  # < this is correctly typed in the IDE
        pass


class ForwardRef:
    def __init__(self, name):
        self.name = name


if __name__ == "__main__":
    my_forward_ref = ForwardRef("name")
    other_class = OtherClass([my_forward_ref])

test_example.py: the test file (just contains an import statement)

import example

We run a test like this

pytest -v --typeguard-packages=:all: test_example.py

The error is:

test_dummy.py:1: in <module>
    import example
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
.venv/lib/python3.11/site-packages/typeguard/_importhook.py:98: in exec_module
    super().exec_module(module)
example.py:5: in <module>
    class OtherClass:
example.py:7: in OtherClass
    def __init__(self, *args: List["ForwardRef"], **kwargs: str) -> None:
E   NameError: name 'ForwardRef' is not defined
============= short test summary info =========
ERROR test_dummy.py - NameError: name 'ForwardRef' is not defined

There is no problem when using these functions normally, even if adding the typechecked decorator.

How can we reproduce the bug?

The files example.py and test_example.py (attached are sufficient to reproduce the above example).

reproduce_error.zip

@manulera manulera added the bug label Jul 19, 2024
@agronholm
Copy link
Owner

How is the run-time type checker supposed to get the ForwardRef name when it's not defined? The error is accurate, as the name is only defined when example.py is imported as __main__.

@manulera
Copy link
Author

manulera commented Nov 4, 2024

Hi @agronholm, thanks for the followup. The source of the error is importing the example module. The __main__ content was just there to show that when running the script normally (python example.py) no error arises. Anyway, you can reproduce the same error omitting the __main__ function.

example.py

from typing import List

class OtherClass:
    def __init__(self, *args: List["ForwardRef"], **kwargs: str) -> None:
        args[0][0].name  # < this is correctly typed in the IDE
        pass


class ForwardRef:
    def __init__(self, name):
        self.name = name

test_example.py

import example

Calling pytest --typeguard-packages=:all: test_example.py gives:

_____________________________________________ ERROR collecting test_example.py ______________________________________________
test_example.py:1: in <module>
    from example import OtherClass, ForwardRef
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
.venv/lib/python3.11/site-packages/typeguard/_importhook.py:98: in exec_module
    super().exec_module(module)
example.py:4: in <module>
    class OtherClass:
example.py:5: in OtherClass
    def __init__(self, *args: List["ForwardRef"], **kwargs: str) -> None:
E   NameError: name 'ForwardRef' is not defined
================================================== short test summary info ==================================================
ERROR test_example.py - NameError: name 'ForwardRef' is not defined
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================== 1 error in 0.05s ======================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants