Skip to content

relationship_writer: properly access __annotations__ dict #858

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gotmax23
Copy link

@gotmax23 gotmax23 commented Jul 13, 2025

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

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()` 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
Signed-off-by: Maxwell G <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant