Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-125618: Make FORWARDREF format succeed more often#132818

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 11 commits intopython:mainfromJelleZijlstra:fwdref-format
May 4, 2025

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedApr 22, 2025
edited
Loading

@DavidCEllis
Copy link
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 evenpotentially 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
Copy link
MemberAuthor

Thanks! This was a silly bug that also broketest_pydoc, I will add your case as a test case so we catch this.

@JelleZijlstraJelleZijlstra merged commitaf5799f intopython:mainMay 4, 2025
39 checks passed
@JelleZijlstraJelleZijlstra deleted the fwdref-format branchMay 4, 2025 22:22
diegorusso added a commit to diegorusso/cpython that referenced this pull requestMay 4, 2025
* origin/main:pythongh-125618: Make FORWARDREF format succeed more often (python#132818)
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[3.14] annotationlib - get_annotations returns an empty annotations dict if anAttributeError is raised when__annotations__ is accessed
2 participants
@JelleZijlstra@DavidCEllis

[8]ページ先頭

©2009-2025 Movatter.jp