-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
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
base: main
Are you sure you want to change the base?
Conversation
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 |
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.
Do we have tests that reflect this behavior? If not, please add some to verify this behavior is working as expected :)
17bd38f
to
1f23c41
Compare
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 ( |
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.
No need to resort the imports
except Exception: | ||
if not is_forwardref_format: | ||
raise | ||
new_locals = _StringifierDict( | ||
{**builtins.__dict__, **locals}, |
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.
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): |
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 test also passes for me on main without your changes.
This fix seemingly preserves type parameter semantics for simple tests - unsure if there is more complex behaviour that it may break.
ref.evaluate(format=Format.FORWARDREF)
returns a ForwardRef with a copied__globals__
that no longer updates #137969