Skip to content

get_type_hints() fails on TypedDict inheriting from a generic TypedDict under PEP 563 #138949

@JelleZijlstra

Description

@JelleZijlstra

Bug report

Bug description:

This fails:

from __future__ import annotations
import typing
class TD1[T](typing.TypedDict):
    a: T

class TD2(TD1):
    pass

print(typing.get_type_hints(TD2))
$ uv run --python=3.13 sample3.py 
Traceback (most recent call last):
  File "/Users/jelle/py/cpython/sample3.py", line 9, in <module>
    print(typing.get_type_hints(TD2))
          ~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 2461, in get_type_hints
    value = _eval_type(value, base_globals, base_locals, base.__type_params__)
  File "/opt/homebrew/Cellar/[email protected]/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 474, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 1081, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'T' is not defined

Also reproduces on 3.12 and on the latest RC of 3.14.

(Got here while investigating #137226 (comment), but since this variant is a longstanding bug we don't need to fix it for 3.14.0. I want to record and eventually fix it though.)

CPython versions tested on:

CPython main branch, 3.15, 3.14, 3.13, 3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard 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