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?