tools-python icon indicating copy to clipboard operation
tools-python copied to clipboard

relationship_writer: properly access __annotations__ dict

Open gotmax23 opened this issue 5 months ago • 1 comments

As of PEP 749 (Python 3.14), it is no longer possible to access __annotations__ on class instances. __annotations__ is now a descriptor that is only defined on type and not on object. Apparently, accessing __annotations__ on class instances was never supported in the first place:

Second, in previous versions of Python it was possible to access the __annotations__ attribute on instances of user-defined classes with annotations. However, this behavior was undocumented and not supported by inspect.get_annotations(), and it cannot be preserved under the PEP 649 framework without bigger changes, such as a new object.__annotations__ descriptor. This behavior change should be called out in porting guides.

Note that making this code call dataclasses.fields() (or using the proper helper function in the stdlib to get type annotations) to get a list of fields is probably a better solution here, but I chose the most minimal change to get this working with Python 3.14. Feel free to close this and create a new PR if you prefer a different solution.

Ref: https://peps.python.org/pep-0749/#metaclass-behavior-with-pep-649

gotmax23 avatar Jul 13 '25 20:07 gotmax23

@armintaenzertng gentle ping on this :)

AyanSinhaMahapatra avatar Nov 24 '25 12:11 AyanSinhaMahapatra