`get_type_hints` occurs NameError only if import `__future__.annotations`
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 28/100
Research direction
Reproduce both examples and compare how typing.get_type_hints resolves the local function's return annotation with and without future.annotations. Then inspect the get_type_hints implementation and its tests for generic aliases and deferred annotations; done means the expected behavior and any supported workaround are established in the issue or covered by a targeted regression test.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·