Skip to content
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

get_type_hints throws for nested class with __future__ annotations #86995

Closed
Prometheus3375 mannequin opened this issue Jan 5, 2021 · 3 comments
Closed

get_type_hints throws for nested class with __future__ annotations #86995

Prometheus3375 mannequin opened this issue Jan 5, 2021 · 3 comments
Labels
3.9 only security fixes

Comments

@Prometheus3375
Copy link
Mannequin

Prometheus3375 mannequin commented Jan 5, 2021

BPO 42829
Nosy @gvanrossum, @Prometheus3375
Files
  • annotations.py: Quick representation of the problem
  • 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 = None
    closed_at = <Date 2021-01-05.18:41:01.757>
    created_at = <Date 2021-01-05.11:18:32.558>
    labels = ['3.9']
    title = 'get_type_hints throws for nested class with __future__ annotations'
    updated_at = <Date 2021-01-05.18:41:01.754>
    user = 'https://github.com/Prometheus3375'

    bugs.python.org fields:

    activity = <Date 2021-01-05.18:41:01.754>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-01-05.18:41:01.757>
    closer = 'gvanrossum'
    components = []
    creation = <Date 2021-01-05.11:18:32.558>
    creator = 'Prometheus3375'
    dependencies = []
    files = ['49719', '49720']
    hgrepos = []
    issue_num = 42829
    keywords = []
    message_count = 3.0
    messages = ['384381', '384386', '384423']
    nosy_count = 2.0
    nosy_names = ['gvanrossum', 'Prometheus3375']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42829'
    versions = ['Python 3.9']

    @Prometheus3375
    Copy link
    Mannequin Author

    Prometheus3375 mannequin commented Jan 5, 2021

    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.

    @Prometheus3375 Prometheus3375 mannequin added 3.9 only security fixes labels Jan 5, 2021
    @Prometheus3375
    Copy link
    Mannequin Author

    Prometheus3375 mannequin commented Jan 5, 2021

    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.

    @gvanrossum
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant