GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_type_hints`` by AlexWaygood · Pull Request #118009 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Apr 17, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled #114053

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
pythongh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_ty…
c76bfc0
…pe_hints``
@AlexWaygood
AlexWaygood requested a review from JelleZijlstra as a code owner April 17, 2024 21:02
@bedevere-app bedevere-app Bot mentioned this pull request Apr 17, 2024
typing.get_type_hints fails when passed a class with PEP 695 type parameters and PEP 563 is enabled #114053
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 17, 2024
@AlexWaygood AlexWaygood added the needs backport to 3.12 only security fixes label Apr 17, 2024
AlexWaygood
AlexWaygood commented Apr 17, 2024
View reviewed changes
Comment thread Lib/typing.py Outdated
eval(
self.__forward_code__,
globalns,
{param.__name__: param for param in type_params} | localns

@AlexWaygood AlexWaygood Apr 17, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
Docs
edd89d7
AlexWaygood
AlexWaygood commented Apr 17, 2024
View reviewed changes
Comment thread Doc/library/typing.rst
Comment on lines +3027 to +3029
them in ``globals``, ``locals`` and (where applicable)
:ref:`type parameter <annotation-scopes>` namespaces.
For a class ``C``, return

@AlexWaygood AlexWaygood Apr 17, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
Improve tests and add a comment
3c56a61
JelleZijlstra
JelleZijlstra reviewed Apr 19, 2024
View reviewed changes
Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra approved these changes Apr 19, 2024
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 19, 2024
@AlexWaygood @JelleZijlstra
Update Doc/library/typing.rst
a7f6f82
Co-authored-by: Jelle Zijlstra <[email protected]>
@AlexWaygood
AlexWaygood enabled auto-merge (squash) April 19, 2024 12:23
@AlexWaygood
AlexWaygood merged commit 1e3e7ce into python:main Apr 19, 2024
@AlexWaygood
AlexWaygood deleted the gettypehints-695 branch April 19, 2024 13:03
@miss-islington-app

miss-islington-app Bot commented Apr 19, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 19, 2024
@miss-islington-app

miss-islington-app Bot commented Apr 19, 2024

Copy link
Copy Markdown

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
AlexWaygood reacted with thumbs down emoji
All reactions
  • 👎 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned AlexWaygood Apr 19, 2024
@bedevere-app

bedevere-app Bot commented Apr 19, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Apr 19, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this pull request Apr 19, 2024
@AlexWaygood @JelleZijlstra
[3.12] pythongh-114053: Fix bad interaction of PEP-695, PEP-563 and `…
58a86e5
…`get_type_hints`` (python#118009)

(cherry-picked from commit 1e3e7ce)

Co-authored-by: Jelle Zijlstra <[email protected]>
AlexWaygood added a commit that referenced this pull request Apr 19, 2024
@AlexWaygood @JelleZijlstra
[3.12] gh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_t…
5430f61
…ype_hints`` (#118009) (#118104)

Co-authored-by: Jelle Zijlstra <[email protected]>
@domdfcoding domdfcoding mentioned this pull request May 15, 2024
generate_self_schema.py incompatible with Python 3.13.0b1 pydantic/pydantic-core#1292
Closed
@alexmojaki alexmojaki mentioned this pull request Jun 3, 2024
"NameError: name 'T' is not defined" when using "from __future__ import annotations" with generic function pydantic/pydantic#9536
Closed
1 task done
@AlexWaygood AlexWaygood mentioned this pull request Jun 8, 2024
gh-114053: Fix another edge case involving get_type_hints, PEP 695 and PEP 563 #120272
Merged
vfazio added a commit to vfazio/pydantic that referenced this pull request Jun 10, 2024
@vfazio
Specify recursive_guard as kwarg in FutureRef._evaluate
0a1b647
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 <[email protected]>
vfazio added a commit to vfazio/pydantic that referenced this pull request Jun 10, 2024
@vfazio
Specify recursive_guard as kwarg in FutureRef._evaluate
78c0401
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 <[email protected]>
@vfazio vfazio mentioned this pull request Jun 10, 2024
Specify recursive_guard as kwarg in FutureRef._evaluate pydantic/pydantic#9612
Merged
5 tasks
@alangenfeld alangenfeld mentioned this pull request Jul 12, 2024
[check builder] 3.12.4 fix dagster-io/dagster#22986
Merged
alangenfeld pushed a commit to dagster-io/dagster that referenced this pull request Jul 12, 2024
Alex Langenfeld
[check builder] 3.12.4 fix (#22986)
b3e0cd4
`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

dimaqq commented Aug 1, 2024

Copy link
Copy Markdown
Contributor

cc for 3.12 backport:

https://discuss.python.org/t/3-12-3-3-12-4-regression-deep-rabbit-hole-warning/59584/12

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin devdanzin mentioned this pull request Aug 1, 2024
TypeVar with bad arguments segfault/misbehavior #118814
Closed
@Viicos Viicos mentioned this pull request Jan 8, 2025
TypeError: ForwardRef._evaluate() got an unexpected keyword argument 'type_params' pydantic/pydantic#11230
Closed
5 of 15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@AlexWaygood AlexWaygood

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AlexWaygood @dimaqq @JelleZijlstra
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.