-
Notifications
You must be signed in to change notification settings - Fork 1
Typechecking fails for recursive type annotations #6
Copy link
Copy link
Open
Description
Consider the following:
from __future__ import annotations
from sumtype import sumtype
class Test(sumtype):
def A(inner: Test): ...This results in the following NameError exception being raised:
Traceback (most recent call last):
File "scratch.py", line 4, in <module>
class Test(sumtype):
^^^^^^^^^^^^^^^^^^^^
File "...\Lib\site-packages\sumtype\sumtype_meta.py", line 65, in __new__
hints = typing.get_type_hints(constructor_stub)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\Lib\typing.py", line 2315, in get_type_hints
hints[name] = _eval_type(value, globalns, localns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\Lib\typing.py", line 365, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\Lib\typing.py", line 842, in _evaluate
eval(self.__forward_code__, globalns, localns),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
NameError: name 'Test' is not defined
The future import is supposed to help avoid this problem by delaying the evaluation of all type annotations, but since you are explicitly calling typing.get_type_hints(), I believe you are effectively breaking that functionality. Further, using @typeguard_ignore does not fix this issue either.
This recursive definition is a common way to encode things like parse trees (which is exactly my current use case).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels