You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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__.
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
fromtypingimportListclassOtherClass:
def__init__(self, *args: List["ForwardRef"], **kwargs: str) ->None:
args[0][0].name# < this is correctly typed in the IDEpassclassForwardRef:
def__init__(self, name):
self.name=name
_____________________________________________ 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 ======================================================
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 filetest_example.py
: the test file (just contains an import statement)We run a test like this
The error is:
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
The text was updated successfully, but these errors were encountered: