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

typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled #114053

Closed
mjog opened this issue Jan 14, 2024 · 6 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@mjog
Copy link

mjog commented Jan 14, 2024

Bug report

Bug description:

from __future__ import annotations

import typing

class Test[M]:
    attr: M

print(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:

{'attr': M}

Python version:

$ poetry run python3 --version
Python 3.12.0

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@mjog mjog added the type-bug An unexpected behavior, bug, or error label Jan 14, 2024
@Eclips4 Eclips4 added topic-typing 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jan 14, 2024
@mjog

This comment was marked as outdated.

@mjog mjog 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 Jan 14, 2024
@mjog mjog 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 Jan 15, 2024
@mjog
Copy link
Author

mjog commented 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
Copy link

+1, just ran into this as well.

@AlexWaygood
Copy link
Member

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.

AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Apr 17, 2024
AlexWaygood added a commit that referenced this issue Apr 19, 2024
…ts`` (#118009)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Apr 19, 2024
…`get_type_hints`` (python#118009)

(cherry-picked from commit 1e3e7ce)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
AlexWaygood added a commit that referenced this issue Apr 19, 2024
…ype_hints`` (#118009) (#118104)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Apr 19, 2024
@treykeown
Copy link

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

@AlexWaygood
Copy link
Member

The bug was fixed in typing.get_type_hints, but the same bug also exists in inspect.get_annotations when eval_str=True is passed:

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

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 13, 2024
….get_annotations` (pythonGH-120270)

(cherry picked from commit 42351c3)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 13, 2024
….get_annotations` (pythonGH-120270)

(cherry picked from commit 42351c3)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Jun 13, 2024
…t.get_annotations` (GH-120270) (#120475)

gh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect.get_annotations` (GH-120270)
(cherry picked from commit 42351c3)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Jun 13, 2024
…t.get_annotations` (GH-120270) (#120474)

gh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect.get_annotations` (GH-120270)
(cherry picked from commit 42351c3)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 25, 2024
…P 695 and PEP 563 (pythonGH-120272)

(cherry picked from commit 2d3187b)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 25, 2024
…P 695 and PEP 563 (pythonGH-120272)

(cherry picked from commit 2d3187b)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Jun 25, 2024
…EP 695 and PEP 563 (GH-120272) (#121003)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Jun 25, 2024
…EP 695 and PEP 563 (GH-120272) (#121004)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants