You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
annotationlib: ref.evaluate() not working for generics as local variables #138151
Usually, when a local variable is introduced after getting a ForwardRef, evaluate() picks this up and correctly evaluates it using the self.__cell__ short-circuit. However, this does not work when that local variable is a generic. It does still work for globals.
I wouldn't expect your fails() to work: you set sequence_b only in that function's local scope, and that's not the scope the annotation was defined in.
I wouldn't expect your fails() to work: you set sequence_b only in that function's local scope, and that's not the scope the annotation was defined in.
Thanks, this is a good point, I forgot to consider that when writing up the example.
I've edited the OP with more complete examples, and put the classes inside the function definitions so the scoping should be correct.
The problem here is that we don't properly preserve a reference to the cell object that was used to create the ForwardRef. This should be done when we create the ForwardRef (i.e., in get_annotations or code called from it), not in ForwardRef.evaluate. We can probably fix this by putting more information in the __cell__ attribute of the ForwardRef.
Activity
JelleZijlstra commentedon Aug 26, 2025
I wouldn't expect your
fails()to work: you setsequence_bonly in that function's local scope, and that's not the scope the annotation was defined in.dr-carlos commentedon Aug 26, 2025
Thanks, this is a good point, I forgot to consider that when writing up the example.
I've edited the OP with more complete examples, and put the classes inside the function definitions so the scoping should be correct.
JelleZijlstra commentedon Aug 26, 2025
The problem here is that we don't properly preserve a reference to the cell object that was used to create the ForwardRef. This should be done when we create the ForwardRef (i.e., in get_annotations or code called from it), not in ForwardRef.evaluate. We can probably fix this by putting more information in the
__cell__attribute of the ForwardRef.pythongh-138151: Fix annotationlib handling of multiple nonlocals
dr-carlos commentedon Aug 27, 2025
@JelleZijlstra Is #138161 still necessary given the broader fix in #138164 , or should I close it?
JelleZijlstra commentedon Aug 27, 2025
I don't think #138161 is necessary, but someone will need to review #138164.
gh-138151: Fix annotationlib handling of multiple nonlocals (#138164)
pythongh-138151: Fix annotationlib handling of multiple nonlocals (py…
[3.14] gh-138151: Fix annotationlib handling of multiple nonlocals (G…
efimov-mikhail commentedon Nov 3, 2025
I've checked that merged PRs fix this issue on main and 3.14. So I'll close this.
pythongh-138151: Fix annotationlib handling of multiple nonlocals (py…