Skip to content

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

Description

@mjog

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jan 14, 2024
  2. mjog commented on Jan 14, 2024

    @mjog
    Author
  3. 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
  4. 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
  5. mjog commented on Jan 15, 2024

    @mjog
    Author

    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.

  6. treykeown commented on Jan 19, 2024

    @treykeown

    +1, just ran into this as well.

  7. AlexWaygood commented on Apr 17, 2024

    @AlexWaygood
    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.

  8. added 4 commits that reference this issue on Apr 17, 2024
  9. 9 remaining items

  10. added a commit that references this issue on Jun 13, 2024
  11. added 2 commits that reference this issue on Jun 13, 2024
  12. added a commit that references this issue on Jun 25, 2024
  13. added 2 commits that reference this issue on Jun 25, 2024
  14. added 2 commits that reference this issue on Jun 25, 2024
  15. added 2 commits that reference this issue on Jun 30, 2024
  16. added 2 commits that reference this issue on Jul 11, 2024
  17. added 2 commits that reference this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions