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-148680: Replace internal names with type_reprs of objects in string representations of ForwardRef by DavidCEllis · Pull Request #148682 · python/cpython

Conversation

@DavidCEllis

@DavidCEllis DavidCEllis commented Apr 17, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This adds a new ForwardRef.__resolved_forward_str__ attribute which takes the __forward_arg__ and replaces any internal __annotationlib_name_x__ names with the type_repr of the object they represent.

Before:

ForwardRef('ref | __annotationlib_name_1__', is_class=True, owner=<class '__main__.Example'>)
ref | __annotationlib_name_1__

After:

ForwardRef('ref | str', is_class=True, owner=<class '__main__.Example'>)
ref | str

This approach replaces the extra_names with their type_repr when the forwardref is evaluated to a string. I'm fairly sure that trying to get the original names as they are in the source would be both slower and significantly more complicated.

This logic is largely based on something I already have for my reannotate library.

  • Issue: annotationlib - evaluating forward references in STRING format can 'leak' internal names #148680

Sorry, something went wrong.

Uh oh!

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

All reactions
DavidCEllis added 3 commits April 17, 2026 12:52
@DavidCEllis
Add tests for replacement of extra names in repr and string evaluation
12d7a45
@DavidCEllis
Replace names from __extra_names__ when evaluating forward references…
e1e258e
… as strings
@DavidCEllis
Try to write a clearer comment
ade5f8e
@DavidCEllis
DavidCEllis requested a review from JelleZijlstra as a code owner April 17, 2026 12:37
@bedevere-app bedevere-app Bot added the awaiting review label Apr 17, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 17, 2026
annotationlib - evaluating forward references in STRING format can 'leak' internal names #148680
Closed
JelleZijlstra
JelleZijlstra reviewed Apr 17, 2026
View reviewed changes
Comment thread Lib/test/test_annotationlib.py Resolved

Uh oh!

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

JelleZijlstra
JelleZijlstra reviewed Apr 17, 2026
View reviewed changes
Comment thread Lib/annotationlib.py
resolved_str = type_repr(name_obj)
else:
visitor = _ExtraNameFixer(names)
ast_expr = ast.parse(resolved_str, mode="eval").body

@JelleZijlstra JelleZijlstra Apr 17, 2026

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.

We might need to cache this, it's probably pretty slow. What do you think?

Sorry, something went wrong.

Uh oh!

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

All reactions

@DavidCEllis DavidCEllis Apr 17, 2026

Copy link
Copy Markdown
Contributor 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 is in fact, not very fast. Definitely worth caching if it's going to be accessed frequently.

Sorry, something went wrong.

Uh oh!

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

All reactions

@DavidCEllis DavidCEllis Apr 17, 2026

Copy link
Copy Markdown
Contributor 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.

>>> a_anno
ForwardRef('unknown | str | int | list[str] | tuple[int, ...]', is_class=True, owner=<class '__main__.Example'>)
>>> b_anno
ForwardRef('unknown', is_class=True, owner=<class '__main__.Example'>)

>>> a = timeit(lambda: a_anno.__resolved_forward_str__, number=10_000)
>>> b = timeit(lambda: b_anno.__resolved_forward_str__, number=10_000)
>>> a
0.18359258100008446
>>> b
0.0018204959997092374
>>> a / b
100.8475607907994

Sorry, something went wrong.

Uh oh!

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

All reactions

@DavidCEllis DavidCEllis Apr 20, 2026

Copy link
Copy Markdown
Contributor 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.

I've added a cache and ended up shortening the name of the property and the cache. Adding the cache did mean adding an extra slot to both classes.

Not completely sold on the names I have if you have something better.

Sorry, something went wrong.

Uh oh!

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

All reactions
DavidCEllis added 2 commits April 17, 2026 16:15
@DavidCEllis
Make the repr test use retrieved rather than 'artificial' forward ref…
f3b6293
…erences
@DavidCEllis
Add a cache for resolved str, test the cache is used, shorten names
675738a
ashm-dev
ashm-dev reviewed Apr 20, 2026
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.

DavidCEllis and others added 4 commits April 20, 2026 22:48
@DavidCEllis @ashm-dev
Update Lib/annotationlib.py
5a36751
Fix a misplaced parenthesis that incorrectly caused a statement to evaluate as truthy

Co-authored-by: Shamil <[email protected]>
@DavidCEllis
remove unnecessary and incorrect node 'fixup'.
3d9b64a
@DavidCEllis
Remove optimization for a condition that should never be hit normally
57bfa41
@DavidCEllis
Remove 'synthetic' test, make the regular test replace additional values
3d08f8f
JelleZijlstra
JelleZijlstra approved these changes Apr 23, 2026
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.

Looks good, but could you add a NEWS entry?

We'll also have to decide whether to backport to 3.14. I'm leaning towards yes, because the old behavior seems genuinely buggy.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Apr 23, 2026
blurb-it Bot and others added 2 commits April 23, 2026 07:38
@blurb-it
📜🤖 Added by blurb_it.
5de5e01
@DavidCEllis
Fix linting issue (maybe)
4ff6eeb
@JelleZijlstra JelleZijlstra added the needs backport to 3.14 bugs and security fixes label Apr 23, 2026
@JelleZijlstra
JelleZijlstra merged commit 158dbbb into python:main Apr 23, 2026
53 checks passed

Uh oh!

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

@miss-islington-app

miss-islington-app Bot commented Apr 23, 2026

Copy link
Copy Markdown

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

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 23, 2026
@bedevere-app

bedevere-app Bot commented Apr 23, 2026

Copy link
Copy Markdown

GH-148913 is a backport of this pull request to the 3.14 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.14 bugs and security fixes label Apr 23, 2026
JelleZijlstra pushed a commit that referenced this pull request Apr 23, 2026
@miss-islington @DavidCEllis @ashm-dev
[3.14] gh-148680: Replace internal names with type_reprs of objects i…
b6a7212
…n string representations of ForwardRef (GH-148682) (#148913)

(cherry picked from commit 158dbbb)

Co-authored-by: David Ellis <[email protected]>
Co-authored-by: Shamil <[email protected]>
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@DavidCEllis @ashm-dev @ljfp
pythongh-148680: Replace internal names with type_reprs of objects in…
2848310
… string representations of ForwardRef (python#148682)

Co-authored-by: Shamil <[email protected]>
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
+1 more reviewer
@ashm-dev ashm-dev ashm-dev left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

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

@DavidCEllis @JelleZijlstra @ashm-dev
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.