Skip to content

gh-137969: Fix evaluation of ref.evaluate(format=Format.FORWARDREF) objects #138075

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dr-carlos
Copy link

@dr-carlos dr-carlos commented Aug 22, 2025

This fix seemingly preserves type parameter semantics for simple tests - unsure if there is more complex behaviour that it may break.

@python-cla-bot
Copy link

python-cla-bot bot commented Aug 22, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 22, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

sharktide

This comment was marked as duplicate.

Copy link
Contributor

@sharktide sharktide left a comment

Choose a reason for hiding this comment

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

Do we have tests that reflect this behavior? If not, please add some to verify this behavior is working as expected :)

@dr-carlos dr-carlos force-pushed the forwardref-double-anno branch from 17bd38f to 1f23c41 Compare August 23, 2025 00:41
@dr-carlos
Copy link
Author

Do we have tests that reflect this behavior? If not, please add some to verify this behavior is working as expected :)

Just added tests that re-evaluation works for new locals and globals, and that it still correctly raises a NameError.

import typing
import unittest
from annotationlib import (
Copy link
Member

Choose a reason for hiding this comment

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

No need to resort the imports

except Exception:
if not is_forwardref_format:
raise
new_locals = _StringifierDict(
{**builtins.__dict__, **locals},
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR but putting the builtins here seems wrong, wouldn't this give the builtins preference over the globals? We can sort that out in another PR though.

@@ -1683,6 +1678,28 @@ def test_fwdref_invalid_syntax(self):
with self.assertRaises(SyntaxError):
fr.evaluate()

def test_re_evaluate(self):
Copy link
Member

Choose a reason for hiding this comment

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

This test also passes for me on main without your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants