10000 get_type_hints() always raises on forward references · Issue #508 · python/typing · GitHub
[go: up one dir, main page]

Skip to content
get_type_hints() always raises on forward references #508
Closed
@ilevkivskyi

Description

@ilevkivskyi

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]}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0