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-119180: Avoid going through AST and eval() when possible in annotationlib by JelleZijlstra · Pull Request #124337 · python/cpython

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Sep 23, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Often, ForwardRefs represent a single simple name. In that case, we can avoid going through the overhead of creating AST nodes and code objects and calling eval(): we can simply look up the name directly in the relevant namespaces.

This PR incorporates the changes from #124326 (which fixes a bug that I discovered while working on this change).

  • Issue: Implement PEP 649 and PEP 749 #119180

Sorry, something went wrong.

Uh oh!

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

All reactions
JelleZijlstra added 4 commits September 22, 2024 14:25
@JelleZijlstra
pythongh-119180: Fix bug where fwdrefs were evaluated in the annotati…
293fb0a
…onlib module scope

We were sometimes passing None as the globals argument to eval(), which makes it
inherit the globals from the calling scope. Instead, ensure that globals is always
non-None. The test was passing accidentally because I passed "annotationlib" as a
module object; fix that. Also document the parameters to ForwardRef() and remove
two unused private ones.
@JelleZijlstra
pythongh-119180: Optimize annotationlib to avoid creation of AST node…
3cfa504
…s and eval()

Often, ForwardRefs represent a single simple name. In that case, we
can avoid going through the overhead of creating AST nodes and code
objects and calling eval(): we can simply look up the name directly
in the relevant namespaces.
@JelleZijlstra
add a test case
eaea393
@JelleZijlstra
more tests
e274f99
@JelleZijlstra JelleZijlstra added the skip news label Sep 23, 2024
@JelleZijlstra
JelleZijlstra requested a review from AlexWaygood September 23, 2024 14:24
@bedevere-app bedevere-app Bot mentioned this pull request Sep 23, 2024
Implement PEP 649 and PEP 749 #119180
Closed
21 of 30 tasks
JelleZijlstra added 2 commits September 23, 2024 07:34
@JelleZijlstra
fmt
0dc0a68
@JelleZijlstra
Merge branch 'main' into pep649-optanno
2f5d504
vstinner
vstinner reviewed Sep 24, 2024
View reviewed changes
Comment thread Lib/test/test_annotationlib.py Resolved

Uh oh!

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

JelleZijlstra added 2 commits September 24, 2024 10:09
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-optanno
892efe9
@JelleZijlstra
one more test
fefa126
@JelleZijlstra
JelleZijlstra marked this pull request as ready for review September 24, 2024 17:22
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 24, 2024
vstinner
vstinner approved these changes Sep 24, 2024
View reviewed changes

@vstinner vstinner left a comment •
edited
Loading

Uh oh!

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

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.

LGTM. I just have minor coding style suggestions that you can ignore if you prefer.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_annotationlib.py Resolved

Uh oh!

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

Comment thread Lib/annotationlib.py Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Sep 24, 2024
@JelleZijlstra @vstinner
Apply suggestions from code review
ab30124
Co-authored-by: Victor Stinner <[email protected]>
@JelleZijlstra

JelleZijlstra commented Sep 24, 2024

Copy link
Copy Markdown
Member Author

Thanks @vstinner for the review!

All reactions

Sorry, something went wrong.

Uh oh!

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

AlexWaygood
AlexWaygood reviewed Sep 25, 2024
View reviewed changes
Comment thread Lib/annotationlib.py Resolved

Uh oh!

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

Comment thread Lib/annotationlib.py Resolved

Uh oh!

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

JelleZijlstra added 2 commits September 25, 2024 13:40
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-optanno
4b1214e
@JelleZijlstra
PR feedback
cebb78e
@JelleZijlstra
JelleZijlstra requested a review from AlexWaygood September 25, 2024 20:50
AlexWaygood
AlexWaygood approved these changes Sep 25, 2024
View reviewed changes
@JelleZijlstra
JelleZijlstra enabled auto-merge (squash) September 25, 2024 20:52
@JelleZijlstra
JelleZijlstra merged commit 17a544b into python:main Sep 25, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 25, 2024
emilyemorehouse added a commit to lysnikolaou/cpython that referenced this pull request Sep 26, 2024
@emilyemorehouse
Merge branch 'main' into fix-help-double-prompt
4cdb3ae
* main: (69 commits)
  Add "annotate" SET_FUNCTION_ATTRIBUTE bit to dis. (python#124566)
  pythongh-124412: Add helpers for converting annotations to source format (python#124551)
  pythongh-119180: Disallow instantiation of ConstEvaluator objects (python#124561)
  For-else deserves its own section in the tutorial (python#123946)
  Add 3.13 as a version option to the crash issue template (python#124560)
  pythongh-123242: Note that type.__annotations__ may not exist (python#124557)
  pythongh-119180: Make FORWARDREF format look at __annotations__ first (python#124479)
  pythonGH-58058: Add quick reference for `ArgumentParser` to argparse docs (pythongh-124227)
  pythongh-41431: Add `datetime.time.strptime()` and `datetime.date.strptime()` (python#120752)
  pythongh-102450: Add ISO-8601 alternative for midnight to `fromisoformat()` calls. (python#105856)
  pythongh-124370: Add "howto" for free-threaded Python (python#124371)
  pythongh-121277: Allow `.. versionadded:: next` in docs (pythonGH-121278)
  pythongh-119400:  make_ssl_certs: update reference test data automatically, pass in expiration dates as parameters python#119400  (pythonGH-119401)
  pythongh-119180: Avoid going through AST and eval() when possible in annotationlib (python#124337)
  pythongh-124448: Update Windows builds to use Tcl/Tk 8.6.15 (pythonGH-124449)
  pythongh-123884 Tee of tee was not producing n independent iterators (pythongh-124490)
  pythongh-124378: Update test_ttk for Tcl/Tk 8.6.15 (pythonGH-124542)
  pythongh-124513: Check args in framelocalsproxy_new() (python#124515)
  pythongh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (python#124480)
  Doc: Use ``major.minor`` for documentation distribution archive filenames (python#124489)
  ...
@Viicos Viicos mentioned this pull request Jun 17, 2025
Raise consistent NameError in ForwardRef.evaluate() #135646
Closed
@Viicos Viicos mentioned this pull request Aug 6, 2026
Avoid needlessly accessing ForwardRef.__forward_code__ in evaluate_forward_ref() #155278
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@AlexWaygood AlexWaygood AlexWaygood approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

skip news

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

@JelleZijlstra @vstinner @AlexWaygood
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.