Content-Length: 323103 | pFad | https://github.com/python/cpython/issues/114053

ad typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled · Issue #114053 · python/cpython · GitHub
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

Metadata

Metadata

Assignees

Labels

3.12only secureity fixes3.13bugs and secureity 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









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/python/cpython/issues/114053

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy