Skip to content

get_type_hints occurs NameError only if import __future__.annotations #2132

@ornew

Description

@ornew
from typing import get_type_hints


class A[T]:
    def __init__(self, x: T):
        self.x = x

    def foo(self):
        def bar() -> T:
            return self.x

        print(get_type_hints(bar))


A[int](5).foo()

Output: {'return': T}

from __future__ import annotations  # Add this

from typing import get_type_hints


class A[T]:
    def __init__(self, x: T):
        self.x = x

    def foo(self):
        def bar() -> T:
            return self.x

        print(get_type_hints(bar))


A[int](5).foo()

Error:

NameError: name 'T' is not defined

Is this the expected behavior?

Also, is there a way to make it work if __future__.annotations is imported?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions