Skip to content

get_type_hints fails with NameError for local types with __future__ annotations #145112

@francois-rozet

Description

@francois-rozet

Copied from python/mypy_extensions#73.

The following code crashes, but only if importing annotations. Tested with Python 3.12, 3.13 and 3.14.

from __future__ import annotations

from typing import get_type_hints


def f():
    class X:
        pass

    class Y:
        x: X

    print(get_type_hints(Y))


f()  # NameError: name 'X' is not defined
from typing import get_type_hints


def f():
    class X:
        pass

    class Y:
        x: X

    print(get_type_hints(Y))


f()  # prints {'x': <class '__main__.f.<locals>.X'>}

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