Repository navigation
gh-160948: Fix annotationlib.ForwardRef.evaluate() ignoring explicit globals/locals for closure-cell references - #160952
Open
arpittkhandelwal wants to merge 1 commit into
Conversation
…licit globals/locals for closure-cell references
Member
|
Considering the issue is still under discussion, I'm closing this. |
Member
|
We may keep it open and only change the locals handling. Because the globals one may not be wrong. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ForwardRef.evaluate()had an early return for references holding asingle closure cell (
__cell__is aCellType) that ran before anynamespace handling. As a result, explicitly passed
globals/localswere silently ignored for such references, while every other path in
the same method (plain references, dict-of-cells references) gives
explicit namespaces precedence over the captured scope.
Fix
In
Lib/annotationlib.py(ForwardRef.evaluate):globalsnor
localswas passed, preserving the existing fast path andbehaviour for the common no-argument call.
consulted after
localsandglobals(and beforebuiltins,matching normal scope order), so it acts as a fallback when an
explicitly passed namespace does not provide the name. An empty
cell (
ValueError) falls through exactly as before.No other paths are changed: the dict-of-cells and
__extra_names__handling, type-parameter injection, and the non-identifier
evalpath are untouched.
Tests
Two regression tests in
Lib/test/test_annotationlib.py(
TestForwardRefClass):test_evaluate_cell_with_explicit_namespaces— explicitlocalsand
globalswin over the cell (locals over globals when both aregiven, in both VALUE and FORWARDREF formats); the cell remains the
fallback when the name is absent from the explicit namespaces.
test_evaluate_empty_cell_with_explicit_namespaces— an emptiedcell still raises
NameErrorwith no arguments, explicit namespacesare consulted, and a refilled cell resumes its fallback role without
overriding explicit namespaces.
Both tests fail on the unmodified code and pass with this change.
Results (local build of main @ 83fe630):
test_annotationlib: 123 tests run — SUCCESS (121 pre-existing +2 new).
test_typing: SUCCESS.test_inspect: 1 failure intest_details_option_with_package, which also fails on theunmodified tree in this environment (it compares
inspectCLIoutput containing stdlib file paths); unrelated to this change.
Fixes gh-160948.
Disclosure: this change was drafted with AI assistance and reviewed by the author, who takes responsibility for it.