Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
If there's an annotation with an incorrect attribute access, theAttributeError causesget_annotations to return an empty dictionary for the annotations instead of failing or returning aForwardRef.
importtypingfromannotationlibimportget_annotations,FormatclassExample2:real_attribute:typing.Anyfake_attribute:typing.DoesNotExistnew_ann=get_annotations(Example2,format=Format.FORWARDREF)print(f"{new_ann=}")# This should fail, but instead returns an empty dictvalue_ann=get_annotations(Example2,format=Format.VALUE)print(f"{value_ann=}")string_ann=get_annotations(Example2,format=Format.STRING)print(f"{string_ann=}")
Output
new_ann={}value_ann={}string_ann={'real_attribute': 'typing.Any', 'fake_attribute': 'typing.DoesNotExist'}I think this is due to_get_dunder_annotations catchingAttributeError and returning an empty dict, intended for static types but catching this by mistake.
CPython versions tested on:
3.14
Operating systems tested on:
Linux