gh-125618: Make FORWARDREF format succeed more often#132818
Merged
JelleZijlstra merged 11 commits intopython:mainfrom May 4, 2025
Merged
gh-125618: Make FORWARDREF format succeed more often#132818JelleZijlstra merged 11 commits intopython:mainfrom
JelleZijlstra merged 11 commits intopython:mainfrom
Conversation
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: from annotationlib import get_annotations, Format
def boom():
obj = object()
class RaisesAttributeError:
attriberr: obj.missing
get_annotations(RaisesAttributeError, format=Format.FORWARDREF)
boom()Output: Traceback (most recent call last):
File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 9, in <module>
boom()
~~~~^^
File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 7, in boom
get_annotations(RaisesAttributeError, format=Format.FORWARDREF)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 778, in get_annotations
ann = _get_and_call_annotate(obj, format)
File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 902, in _get_and_call_annotate
ann = call_annotate_function(annotate, format, owner=obj)
File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 629, in call_annotate_function
result = func(Format.VALUE_WITH_FAKE_GLOBALS)
File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 6, in __annotate__
attriberr: obj.missing
^^^^^^^^^^^
File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 380, in __getattr__
return self.__make_new(ast.Attribute(self.__get_ast(), attr))
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 358, in __make_new
self.__stringifier_dict__.stringifiers.append(stringifier)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'builtin_function_or_method' object has no attribute '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). def test_attributeerror_handled(self):
class MissingAttrib:
pass
class RaisesAttributeError:
attriberr: MissingAttrib.missing
with self.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"}
) |
Member
Author
|
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 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.
Fixes #125618.
cc @DavidCEllis
AttributeErroris raised when__annotations__is accessed #125618