You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
same_classes.py: An example when a class and its module have classes with the same name
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-01-05.18:41:01.757>created_at=<Date2021-01-05.11:18:32.558>labels= ['3.9']
title='get_type_hints throws for nested class with __future__ annotations'updated_at=<Date2021-01-05.18:41:01.754>user='https://github.com/Prometheus3375'
If a class X has nested class Y and class X has annotation Y AFTER the definition of Y, get_type_hints(X) throws NameError if import from future annotations is present.
This is because get_type_hints looks only at mro of the class. Its namespace must be included too as locals.
This is because get_type_hints looks only at mro of the class. Its namespace must be included too as locals
<
I'll correct myself. get_type_hints() looks at all entites of mro of a class. If globals are not defined, globals of an entity module are used. But if locals are not defined, then no action is taken. Actually, class namespace must be used as locals in that case.
This is also supported by the case when a class and its module have classes with the same name. See same_classes.py for an example.
If you want this to work you have to pass the namespace where B is defined as locals to get_type_hint(). The recommended approach is to make B a global class; where that is not possible (as in your second example) you should chose a different name.
We're not going to guess additional namespaces to add to the lookup, so I will close this as won't fix.
PS. For short code snippets like your examples, I prefer pasting the code inline.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: