Uh oh!
There was an error while loading. Please reload this page.
gh-125618: Make FORWARDREF format succeed more often - #132818
Merged
Merged
Conversation
DavidCEllis
commented
Apr 23, 2025
Contributor
Looks like this works in the case where the object is defined globally, but this still manages to fail if the object used in the annotation is defined (or even potentially redefined) within the function. Repro: fromannotationlibimportget_annotations, Formatdefboom():
obj=object()
classRaisesAttributeError:
attriberr: obj.missingget_annotations(RaisesAttributeError, format=Format.FORWARDREF)
boom()Output: Traceback (mostrecentcalllast):
File"C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line9, in<module>boom()
~~~~^^File"C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line7, inboomget_annotations(RaisesAttributeError, format=Format.FORWARDREF)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line778, inget_annotationsann=_get_and_call_annotate(obj, format)
File"C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line902, in_get_and_call_annotateann=call_annotate_function(annotate, format, owner=obj)
File"C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line629, incall_annotate_functionresult=func(Format.VALUE_WITH_FAKE_GLOBALS)
File"C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line6, in__annotate__attriberr: obj.missing^^^^^^^^^^^File"C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line380, in__getattr__returnself.__make_new(ast.Attribute(self.__get_ast(), attr))
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line358, in__make_newself.__stringifier_dict__.stringifiers.append(stringifier)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AttributeError: 'builtin_function_or_method'objecthasnoattribute'stringifiers'This came up because this is how I'd written my test case for this (writing the test case was actually as far as I'd gone, because the first test case raised the other issue). deftest_attributeerror_handled(self):
classMissingAttrib:
passclassRaisesAttributeError:
attriberr: MissingAttrib.missingwithself.assertRaises(AttributeError):
get_annotations(RaisesAttributeError, format=Format.VALUE)
self.assertEqual(
get_annotations(RaisesAttributeError, format=Format.FORWARDREF),
{
"attriberr": support.EqualToForwardRef(
"MissingAttrib.missing", owner=RaisesAttributeError,
is_class=True,
)
}
)
self.assertEqual(
get_annotations(RaisesAttributeError, format=Format.STRING),
{"attriberr": "MissingAttrib.missing"}
) |
JelleZijlstra
commented
Apr 23, 2025
MemberAuthor
Thanks! This was a silly bug that also broke |
diegorusso added a commit
to diegorusso/cpython
that referenced
this pull request
May 4, 2025
* origin/main: pythongh-125618: Make FORWARDREF format succeed more often (python#132818)
Pranjal095 pushed a commit
to Pranjal095/cpython
that referenced
this pull request
Jul 12, 2025
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 freeto 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.
Fixes#125618.
cc @DavidCEllis
AttributeErroris raised when__annotations__is accessed #125618