8000 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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ilevkivskyi opened this issue Dec 3, 2017 · 8 comments
Closed

get_type_hints() always raises on forward references #508

ilevkivskyi opened this issue Dec 3, 2017 · 8 comments
Assignees

Comments

@ilevkivskyi
Copy link
Member

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]}
@gvanrossum
Copy link
Member
gvanrossum commented Dec 4, 2017 via email

@djrochford
Copy link

I take it this was not easy to fix? I, for one, would be very glad to see this fixed, but I gather that's unlikely to happen soon...

@ilevkivskyi
Copy link
Member Author

This is not easy, but not too hard either. I just didn't have time for this, sorry.

@djrochford
Copy link

Oh, no need to apologise! I commented only in the hope of bumping this slightly higher the priority list, but I'll survive, and life is short.

@ghost
Copy link
ghost commented Jun 2, 2020

Is there any chance to get this fixed in the near future?

@ghost
Copy link
ghost commented Jun 6, 2020

You can temporarily fix this issue by passing a dict as parameter with the needed classes to the function.

@The-Compiler
Copy link

A somewhat related Python bug I've opened about this a couple of weeks ago (before finding this issue): Issue 43463: typing.get_type_hints with TYPE_CHECKING imports / getting hints for single argument - Python tracker

@srittau
Copy link
Collaborator
srittau commented Nov 4, 2021

Please report this bug to bugs.python.org if it still exists.

@srittau srittau closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0