-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-137969: Fix double evaluation of ForwardRefs which rely on globals
#140974
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
JelleZijlstra
left a comment
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.
Thanks! Could you also add a unit test asserting that if a locals dictionary is passed in, it's not modified?
Lib/annotationlib.py
Outdated
| try: | ||
| globals[cell_name] = cell_value.cell_contents | ||
| if cell_name not in locals: | ||
| locals[cell_name] = cell_value.cell_contents |
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.
Can you narrow the try-except so it only covers this line? (The part that can raise ValueError is the .cell_contents attribute read.)
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.
Hopefully replacing with setdefault is fine - if the object doesn't have a 'setdefault' attribute, it will raise an AttributeError not ValueError.
Added one to test globals and locals, since they're both dicts which are mutable. |
ref.evaluate(format=Format.FORWARDREF)objects #138075, but just adds new locals (without overwriting the actual locals), instead of using a new dictionary.__cell__nonlocal variables added to globals in gh-138151: Fix annotationlib handling of multiple nonlocals #138164globaldeclaration and resets across test runs (this broke buildbots last time).ref.evaluate(format=Format.FORWARDREF)returns a ForwardRef with a copied__globals__that no longer updates #137969