Visitar URL original
bpo-42904: Fix get_type_hints for class local namespaces by Fidget-Spinner · Pull Request #24201 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220507114800/https://github.com/python/cpython/pull/24201
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

bpo-42904: Fix get_type_hints for class local namespaces #24201

Merged
merged 6 commits into from Apr 12, 2021

Conversation

Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Jan 12, 2021 •

Currently this code errors:

from __future__ import annotations
from typing import get_type_hints

class Foo:
    y = str
    x: y

get_type_hints(Foo)

Now it works. An oddity with get_type_hints (noted by others on python-dev) is that it previously seemed to do nothing at all to find the local namespace of a class...

https://bugs.python.org/issue42904

@Fidget-Spinner Fidget-Spinner marked this pull request as draft Jan 13, 2021
@Fidget-Spinner Fidget-Spinner marked this pull request as ready for review Jan 13, 2021
@github-actions
Copy link

@github-actions github-actions bot commented Feb 13, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Feb 13, 2021
@gvanrossum gvanrossum removed the request for review from ilevkivskyi Apr 10, 2021
Lib/test/test_typing.py Show resolved Hide resolved
Lib/typing.py Outdated
base_locals = dict(vars(base)) if localns is None else localns
if localns is None and globalns is None:
# This is surprising, but required. Before Python 3.10,
# get_type_hints only evaluated the globalns of
# a class. To maintain backwards compatibility, we reverse
# the globalns and localns order so that eval() looks into
# *base_globals* first rather than *base_locals*.
base_globals, base_locals = base_locals, base_globals
Copy link
Member

@gvanrossum gvanrossum Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ow, this is indeed weird. I guess it means that if we have

class A: ...
class B:
    x: "A"
    class A: ...

then get_type_hints(B)["x"] will refer to the global A, not to B.A?

I suppose you ran into tests that broke if you didn't swap the variables, and those tests did have both a global and a local definition of a type used in an annotation with the same name? But this seems arbitrary. I tried annotations with and without quotation marks, and before and after the inner 'class A:', and now they all refer to the global A. But arguably they could all refer to B.A.

What do you think? (Specifically what tests would fail without this counter-intuitive swap?)

Copy link
Member Author

@Fidget-Spinner Fidget-Spinner Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember some tests failing if I didn't have this swap. However, now when I remove the swap, all the tests miraculously pass. Perhaps the past me of 3 months ago saw something that the current me doesn't. But now I have no clue what I was doing, so I removed it.

The magic of time allows for tests to magically pass I guess 😕?

Copy link
Member

@gvanrossum gvanrossum left a comment

Glad that was a non-issue! Approved. I will merge it now.

@gvanrossum gvanrossum merged commit 852150d into python:master Apr 12, 2021
11 checks passed
@Fidget-Spinner Fidget-Spinner deleted the get-type-hints-locals branch Apr 13, 2021
@larryhastings
Copy link

@larryhastings larryhastings commented Apr 26, 2021

Does the test for this need to change, given that stringized annotations are no longer the default in Python 3.10? If I understand it correctly, the text in this PR no longer calls eval(), and so is no longer exercising localns.

@Fidget-Spinner
Copy link
Author

@Fidget-Spinner Fidget-Spinner commented Apr 26, 2021 •

Does the test for this need to change, given that stringized annotations are no longer the default in Python 3.10? If I understand it correctly, the text in this PR no longer calls eval(), and so is no longer exercising localns.

Larry, you're absolutely right! Thanks for catching this. I created #25632 to address that (it's just a teeny edit).

Edit: Woops I was wrong, it's not going to be a teeny edit at all :(.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA signed stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants