Skip to content

Python 3.14: Call __annotate__ on type objects to get annotations #810

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

Closed
wants to merge 1 commit into from

Conversation

hroncok
Copy link

@hroncok hroncok commented Feb 4, 2025

Fixes #795

@edgarrmondragon
Copy link
Contributor

This fix works on 3.14.0a6 👍

@edgarrmondragon
Copy link
Contributor

This fix works on 3.14.0a6 👍

But it doesn't on 3.14.0b1 😅

@hroncok
Copy link
Author

hroncok commented May 26, 2025

I was thinking that maybe the solution is to reverse the order (aka look into __annotate__ first), but it makes no difference.

@hroncok
Copy link
Author

hroncok commented May 26, 2025

@JelleZijlstra sorry for bothering you directly, but do you by chance know how to fix this code? I mostly contributed this by trial and error and it no longer works with 3.14.0b1. Thanks.

@@ -149,7 +149,12 @@ def get_class_annotations(obj):
cls_locals = dict(vars(cls))
cls_globals = getattr(sys.modules.get(cls.__module__, None), "__dict__", {})

ann = cls.__dict__.get("__annotations__", {})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just use inspect.get_annotations(cls) which will work on all versions.

You may want to use the FORWARDREF format on 3.14 to support classes with annotations that cannot be fully evaluated. I don't know if that's a design goal for this function.

@JelleZijlstra
Copy link

Retrieving __annotations__ and __annotate__ directly from a class namespace is not supported; use https://docs.python.org/3.14/library/annotationlib.html#annotationlib.get_annotate_from_class_namespace instead.

I realize the code where this happens is in C so what I say might not be straightforward though. Would it be OK for the C code to import annotationlib?

JelleZijlstra added a commit to JelleZijlstra/msgspec that referenced this pull request May 26, 2025
With this change, the tests run for me on a local build of Python 3.14.
There are a lot of failures related to sys.getrefcount() but that seems
to be an unrelated issue.

Closes jcrist#810. Fixes jcrist#651. Fixes jcrist#795.
@JelleZijlstra
Copy link

I proposed an alternative fix: #852.

@hroncok
Copy link
Author

hroncok commented May 26, 2025

Thank you, @JelleZijlstra!

@hroncok hroncok closed this May 26, 2025
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.

Python 3.14.0a3: TypeError: Extra positional arguments provided
3 participants