-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled #114053
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
Comments
This comment was marked as outdated.
This comment was marked as outdated.
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. |
+1, just ran into this as well. |
I suppose the problem is probably that |
…ts`` (#118009) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…`get_type_hints`` (python#118009) (cherry-picked from commit 1e3e7ce) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
I created a temporary workaround based off the fix by @AlexWaygood if anyone needs a hack around this in the meantime: https://gist.github.com/treykeown/65c45b3da330d2293c7fb9d2a46520ed |
The bug was fixed in Python 3.14.0a0 (heads/main:eeb8f67f837, Jun 6 2024, 12:03:13) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> class test[M]:
... attr: M
...
>>> import inspect
>>> inspect.get_annotations(test, eval_str=True)
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
inspect.get_annotations(test, eval_str=True)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/Users/alexw/dev/cpython/Lib/inspect.py", line 286, in get_annotations
value if not isinstance(value, str) else eval(value, globals, locals)
~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
NameError: name 'M' is not defined |
….get_annotations`
…P 695 and PEP 563
….get_annotations` (pythonGH-120270) (cherry picked from commit 42351c3) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
….get_annotations` (pythonGH-120270) (cherry picked from commit 42351c3) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
….get_annotations` (python#120270)
…P 695 and PEP 563 (python#120272)
….get_annotations` (python#120270)
…P 695 and PEP 563 (python#120272)
….get_annotations` (python#120270)
…P 695 and PEP 563 (python#120272)
Bug report
Bug description:
This fails with a NameError:
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:
Python version:
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
get_type_hints
#118009get_type_hints
(#118009) #118104inspect.get_annotations
#120270get_type_hints
, PEP 695 and PEP 563 #120272inspect.get_annotations
(GH-120270) #120474inspect.get_annotations
(GH-120270) #120475get_type_hints
, PEP 695 and PEP 563 (GH-120272) #121003get_type_hints
, PEP 695 and PEP 563 (GH-120272) #121004The text was updated successfully, but these errors were encountered: