Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-133960: Improve typing.evaluate_forward_ref#133961

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 2 commits intopython:mainfromJelleZijlstra:efr2
May 25, 2025

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedMay 13, 2025
edited by github-actionsbot
Loading

As explained in#133960, this removes most of the behavior differences with ForwardRef.evaluate.
The remaining difference is about recursive evaluation of forwardrefs; this is practically useful
in cases where an annotation refers to a type alias that itself is string-valued.

This also improves several edge cases that were previously not handled optimally. For example,
the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() to
evaluate more ForwardRefs in the FORWARDREF format.

This alsofixes#133959 as a side effect, because the buggy behavior in#133959 derives from
evaluate_forward_ref().


📚 Documentation preview 📚:https://cpython-previews--133961.org.readthedocs.build/

As explained inpython#133960, this removes most of the behavior differences with ForwardRef.evaluate.The remaining difference is about recursive evaluation of forwardrefs; this is practically usefulin cases where an annotation refers to a type alias that itself is string-valued.This also improves several edge cases that were previously not handled optimally. For example,the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() toevaluate more ForwardRefs in the FORWARDREF format.This alsofixespython#133959 as a side effect, because the buggy behavior inpython#133959 derives fromevaluate_forward_ref().
Comment on lines +7285 to +7288
A = "str"
ref = ForwardRef('list[A]')
with self.assertRaises(NameError):
typing.evaluate_forward_ref(ref)
Copy link
Member

Choose a reason for hiding this comment

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

remind me why this needs to raiseNameError? Does theglobals argument not default to the globals of the module callingevaluate_forward_ref? I think that's what I'd naively expect.

The docs fortyping.evaluate_forward_ref point me towardshttps://docs.python.org/3.14/library/annotationlib.html#annotationlib.ForwardRef.evaluate for information on these parameters, but the docs there are silent on what happens ifglobals isNone andowner isNone

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It eventually defaults to empty globals. I think that's right for most use cases; usually you'll be evaluating a ForwardRef in a library that came from some user code, and the globals of the module where you're doing the evaluating aren't a particularly good place to look for data.

AlexWaygood reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I'll make a separate PR adding this to the annotationlib docs

AlexWaygood reacted with heart emoji
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstraJelleZijlstraenabled auto-merge (squash)May 25, 2025 17:22
@JelleZijlstraJelleZijlstra merged commit57fef27 intopython:mainMay 25, 2025
43 checks passed
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestMay 25, 2025
As explained inpythonGH-133960, this removes most of the behavior differences with ForwardRef.evaluate.The remaining difference is about recursive evaluation of forwardrefs; this is practically usefulin cases where an annotation refers to a type alias that itself is string-valued.This also improves several edge cases that were previously not handled optimally. For example,the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() toevaluate more ForwardRefs in the FORWARDREF format.This alsofixespythonGH-133959 as a side effect, because the buggy behavior inpythonGH-133959 derives fromevaluate_forward_ref().(cherry picked from commit57fef27)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@bedevere-app
Copy link

GH-134663 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelMay 25, 2025
@JelleZijlstraJelleZijlstra deleted the efr2 branchMay 25, 2025 17:39
JelleZijlstra added a commit that referenced this pull requestMay 25, 2025
…34663)gh-133960: Improve typing.evaluate_forward_ref (GH-133961)As explained inGH-133960, this removes most of the behavior differences with ForwardRef.evaluate.The remaining difference is about recursive evaluation of forwardrefs; this is practically usefulin cases where an annotation refers to a type alias that itself is string-valued.This also improves several edge cases that were previously not handled optimally. For example,the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() toevaluate more ForwardRefs in the FORWARDREF format.This alsofixesGH-133959 as a side effect, because the buggy behavior inGH-133959 derives fromevaluate_forward_ref().(cherry picked from commit57fef27)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit to JelleZijlstra/typing_extensions that referenced this pull requestMay 29, 2025
Refer topython/cpython#133961I copied the tests from Python 3.14. Two don't pass but could probably bemade to pass by backporting more of annotationlib, but that's more thanI think we should do now.Fixespython#608
JelleZijlstra added a commit to python/typing_extensions that referenced this pull requestMay 29, 2025
Refer topython/cpython#133961I copied the tests from Python 3.14. Two don't pass but could probably bemade to pass by backporting more of annotationlib, but that's more thanI think we should do now.Fixes#608
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@AlexWaygoodAlexWaygoodAlexWaygood approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

typing.get_type_hints() raises on invalid types only if they're wrapped in a ForwardRef
2 participants
@JelleZijlstra@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp