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

gh-114053: Fix bad interaction of PEP-695, PEP-563 and get_type_hints #118009

Merged
merged 4 commits into from
Apr 19, 2024

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Apr 17, 2024

Lib/typing.py Outdated
eval(
self.__forward_code__,
globalns,
{param.__name__: param for param in type_params} | localns
Copy link
Member Author

@AlexWaygood AlexWaygood Apr 17, 2024

Choose a reason for hiding this comment

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

this means that any annotations in the "local namespace" of the class override any symbols that are present in the scope due to being listed as a type parameter. That matches the behaviour of __annotations__ at runtime in a module that doesn't use __future__ annotations:

>>> class B[T, *Ts, **P]:
...     T = int
...     Ts = str
...     P = bytes
...     x: T
...     y: Ts
...     z: P
... 
>>> B.__annotations__
{'x': <class 'int'>, 'y': <class 'str'>, 'z': <class 'bytes'>}

I'm doing it at the last possible point here as it feels less risky than doing it in _eval_type.

Comment on lines 3027 to 3029
them in ``globals``, ``locals`` and (where applicable)
:ref:`type parameter <annotation-scopes>` namespaces.
For a class ``C``, return
Copy link
Member Author

@AlexWaygood AlexWaygood Apr 17, 2024

Choose a reason for hiding this comment

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

we don't usually add .. versionchanged notes for bugfixes (and I think this is a bugfix), but I could add one if we think this is a significant enough change to the function's semantics

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks, this is a much simpler fix than I was afraid we'd need.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@AlexWaygood AlexWaygood enabled auto-merge (squash) April 19, 2024 12:23
@AlexWaygood AlexWaygood merged commit 1e3e7ce into python:main Apr 19, 2024
33 checks passed
@AlexWaygood AlexWaygood deleted the gettypehints-695 branch April 19, 2024 13:03
@miss-islington-app
Copy link

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @AlexWaygood, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1e3e7ce11e3b0fc76e981db85d27019d6d210bbc 3.12

@bedevere-app
Copy link

bedevere-app bot commented Apr 19, 2024

GH-118104 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Apr 19, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this pull request Apr 19, 2024
…`get_type_hints`` (python#118009)

(cherry-picked from commit 1e3e7ce)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
AlexWaygood added a commit that referenced this pull request Apr 19, 2024
…ype_hints`` (#118009) (#118104)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
vfazio added a commit to vfazio/pydantic that referenced this pull request Jun 10, 2024
In CPython 3.12.4/3.13, the function signature of `FutureRef._evaluate`
changed such that `recursive_guard` is no longer a positional argument;
it is now keyword only [0][1].

To accomodate this, specify `recursive_guard` as a kwarg. This syntax is
backwards compatible with earlier versions of the function signature.

[0]: python/cpython#118104
[1]: python/cpython#118009

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/pydantic that referenced this pull request Jun 10, 2024
In CPython 3.12.4/3.13, the function signature of `FutureRef._evaluate`
changed such that `recursive_guard` is no longer a positional argument;
it is now keyword only [0][1].

To accommodate this, specify `recursive_guard` as a kwarg. This syntax
is backwards compatible with earlier versions of the function signature.

[0]: python/cpython#118104
[1]: python/cpython#118009

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
alangenfeld added a commit to dagster-io/dagster that referenced this pull request Jul 12, 2024
`recursive_guard` got made keyword arg only in
python/cpython#118009 which released in `3.12.4`
(we run an older version in buildkite so we missed it)

fixes #22985

## How I Tested These Changes

on a fresh 3.12.4 venv
`pytest
python_modules/dagster/dagster_tests/general_tests/test_record.py
python_modules/dagster/dagster_tests/general_tests/check_tests/test_check.py`
which was failing before fix
@dimaqq
Copy link
Contributor

dimaqq commented Aug 1, 2024

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

Successfully merging this pull request may close these issues.

3 participants