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
{{ message }}
Repository navigation
typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled #114053
from __future__ importannotationsimporttypingclassTest[M]:
attr: Mprint(typing.get_type_hints(Test))
This fails with a NameError:
$ poetry run python3 pep_695_get_type_hints.py
Traceback (most recent call last):
File "/home/mjog/pep_695_get_type_hints.py", line 8, in <module>
print(typing.get_type_hints(Test))
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/typing.py", line 2197, in get_type_hints
value = _eval_type(value, base_globals, base_locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/typing.py", line 393, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/typing.py", line 900, in _evaluate
eval(self.__forward_code__, globalns, localns),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
NameError: name 'M' is not defined
Although get_type_hints does not work with imported imported type aliases, this is not imported, so I would expect it to work. Further, the documentation for the function indicates using PEP 563 should /help/, but in this case it actually causes an error.
FWIW, removing the PEP 563 import works fine, giving:
changed the title [-]typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled[/-][+]typing.get_type_hints fails when passed a generic class and PEP 563 is enabled[/+]on Jan 14, 2024
changed the title [-]typing.get_type_hints fails when passed a generic class and PEP 563 is enabled[/-][+]typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled[/+]on Jan 15, 2024
I suppose the problem is probably that get_type_hints looks things up in the global scope but doesn't look things up in the new annotation scopes introduced by PEP-695. The old way of defining TypeVars doesn't suffer from this because TypeVars were defined in the global scope; now they aren't.
Activity
mjog commentedon Jan 14, 2024
[-]typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled[/-][+]typing.get_type_hints fails when passed a generic class and PEP 563 is enabled[/+][-]typing.get_type_hints fails when passed a generic class and PEP 563 is enabled[/-][+]typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled[/+]mjog commentedon Jan 15, 2024
Sorry, my now-hidden comment above is incorrect - it does work fine with pre-695 generic classes, I just had a typo in the code.
This is only an issue with PEP 695 type parameters in conjunction with PEP 563.
treykeown commentedon Jan 19, 2024
+1, just ran into this as well.
AlexWaygood commentedon Apr 17, 2024
I suppose the problem is probably that
get_type_hintslooks things up in the global scope but doesn't look things up in the new annotation scopes introduced by PEP-695. The old way of defining TypeVars doesn't suffer from this because TypeVars were defined in the global scope; now they aren't.pythongh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_ty…
gh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_type_hin…
[3.12] pythongh-114053: Fix bad interaction of PEP-695, PEP-563 and `…
[3.12] gh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_t…
9 remaining items
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
[3.12] gh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspec…
[3.13] gh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspec…
gh-114053: Fix another edge case involving `get_type_hints`, PEP 695 …
pythongh-114053: Fix another edge case involving `get_type_hints`, PE…
pythongh-114053: Fix another edge case involving `get_type_hints`, PE…
[3.13] gh-114053: Fix another edge case involving `get_type_hints`, P…
[3.12] gh-114053: Fix another edge case involving `get_type_hints`, P…
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
pythongh-114053: Fix another edge case involving `get_type_hints`, PE…
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
pythongh-114053: Fix another edge case involving `get_type_hints`, PE…
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
pythongh-114053: Fix another edge case involving `get_type_hints`, PE…