Skip to content

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

Closed
hroncok wants to merge 1 commit intojcrist:mainfrom
hroncok:annotate
Closed

Python 3.14: Call __annotate__ on type objects to get annotations#810
hroncok wants to merge 1 commit intojcrist:mainfrom
hroncok:annotate

Conversation

@hroncok
Copy link
Copy Markdown
Contributor

@hroncok hroncok commented Feb 4, 2025

Fixes #795

@edgarrmondragon
Copy link
Copy Markdown
Contributor

This fix works on 3.14.0a6 👍

@edgarrmondragon
Copy link
Copy Markdown
Contributor

This fix works on 3.14.0a6 👍

But it doesn't on 3.14.0b1 😅

@hroncok
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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.

Comment thread msgspec/_utils.py
cls_locals = dict(vars(cls))
cls_globals = getattr(sys.modules.get(cls.__module__, None), "__dict__", {})

ann = cls.__dict__.get("__annotations__", {})
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

I proposed an alternative fix: #852.

@hroncok
Copy link
Copy Markdown
Contributor Author

hroncok commented May 26, 2025

Thank you, @JelleZijlstra!

@hroncok hroncok closed this May 26, 2025
kumaraditya303 pushed a commit to kumaraditya303/msgspec that referenced this pull request Sep 12, 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.
kumaraditya303 pushed a commit to kumaraditya303/msgspec that referenced this pull request Sep 12, 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.
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