get_type_hints() always raises on forward references · Issue #508 · python/typing (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
ericvsmith/dataclasses#92 (comment) raised an important question (this existing problem is also mentioned in PEP 563). Currently, get_type_hints
will raise a NameError
in situations where it might not be the desired behaviour (e.g. if used by a class decorator). For example:
def modify(cls): types = get_type_hints(cls) # do something with 'cls' and 'types' return cls
@modify class C: linked: 'C'
It looks like it will be useful to have an option to keep forward references that are currently undefined. For example:
class C: x: 'ClassVar[B]'
assert get_type_hints(C, allow_forward=True) == {'x': ClassVar['B']}
class B: ...
assert get_type_hints(C, allow_forward=True) == {'x': ClassVar[B]}