GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing github.com — gh-125618: Make FORWARDREF format succeed more often by JelleZijlstra · Pull Request #132818 · python/cpython Conversation
|
|
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"} ) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks! This was a silly bug that also broke |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
* origin/main: pythongh-125618: Make FORWARDREF format succeed more often (python#132818)
There was an error while loading. Please reload this page.
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
There was an error while loading. Please reload this page.