-
Notifications
You must be signed in to change notification settings - Fork 103
Fix annotations support on 3.14 #852
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
base: main
Are you sure you want to change the base?
Conversation
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.
if (annotations == NULL) { | ||
if (mod->get_annotate_from_class_namespace != NULL) { | ||
PyObject *annotate = PyObject_CallOneArg( | ||
mod->get_annotate_from_class_namespace, info->namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the portable approach, which should continue to work on future Python versions. If you value performance over portability, you can instead inline this function https://github.com/python/cpython/blob/9ddc7c548d45b73c84131e6d75b03c26a3e8b6e8/Lib/annotationlib.py#L824 ; it's just dict operations.
I made it a separate function in CPython so that we can be free to optimize the internal representation of annotate functions in the future. For example, perhaps in 3.15 the class will store just a code object instead of a function.
value = _forward_ref(value) | ||
value = _eval_type(value, cls_locals, cls_globals) | ||
if mapping is not None: | ||
value = _apply_params(value, mapping) | ||
if value is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed _eval_type
so it no longer turns None into NoneType.
I get a copule of
Do you also get those? |
I didn't how, are you running the test suite exactly? I get these failures which all seem related to
To run it I do:
That's the latest tip of the 3.14 branch. |
The failures you post feel like they wouldn't be related to retrieving annotations; the code I'm changing is just in gathering annotations at class creation time, and whatever is happening in those tests is after the class is already created. |
Oh actually this is because of a bug in b1 that I fixed (python/cpython#133701); the fix will be in b2 which is about to go out. I can reproduce it with the following change in the test:
I do think that indicates a bug in msgspec; presumably it shouldn't crash even if people mess with the |
The failures occurred on b1 indeed. |
Fixes at least some of the failures reported in: jcrist#852 (comment) These were exposed by a bug in 3.14b1 where TypedDict reported incorrect `__annotations__` but correct `__required_keys__`. msgspec would crash in this case. The bug is reproducible on earlier Python versions by manually manipulating attributes on a TypedDict class. It's a pretty marginal bug but I would argue the extension should be robust to this sort of edge case.
#853 for that one. |
I opened #854 |
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 #810. Fixes #651. Fixes #795.