Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ typedef struct {
#endif
PyObject *astimezone;
PyObject *re_compile;
PyObject *get_annotate_from_class_namespace;
uint8_t gc_cycle;
} MsgspecState;

Expand Down Expand Up @@ -5814,12 +5815,45 @@ structmeta_is_classvar(

static int
structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly) {
PyObject *annotations = PyDict_GetItemString(
PyObject *annotations = PyDict_GetItemString( // borrowed reference
info->namespace, "__annotations__"
);
if (annotations == NULL) return 0;
if (annotations == NULL) {
if (mod->get_annotate_from_class_namespace != NULL) {
PyObject *annotate = PyObject_CallOneArg(
mod->get_annotate_from_class_namespace, info->namespace
Copy link
Author

@JelleZijlstra JelleZijlstra May 26, 2025

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.

);
if (annotate == NULL) {
return -1;
}
if (annotate == Py_None) {
Py_DECREF(annotate);
return 0;
}
PyObject *format = PyLong_FromLong(1); /* annotationlib.Format.VALUE */
if (format == NULL) {
Py_DECREF(annotate);
return -1;
}
annotations = PyObject_CallOneArg(
annotate, format
);
Py_DECREF(annotate);
Py_DECREF(format);
if (annotations == NULL) {
return -1;
}
}
else {
return 0; // No annotations, nothing to do
}
}
else {
Py_INCREF(annotations);
}

if (!PyDict_Check(annotations)) {
Py_DECREF(annotations);
PyErr_SetString(PyExc_TypeError, "__annotations__ must be a dict");
return -1;
}
Expand Down Expand Up @@ -5869,6 +5903,7 @@ structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly)
}
return 0;
error:
Py_DECREF(annotations);
Py_XDECREF(module_ns);
return -1;
}
Expand Down Expand Up @@ -22225,6 +22260,26 @@ PyInit__core(void)
Py_DECREF(temp_module);
if (st->re_compile == NULL) return NULL;

/* annotationlib.get_annotate_from_class_namespace */
temp_module = PyImport_ImportModule("annotationlib");
if (temp_module == NULL) {
if (PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)) {
// Below Python 3.14
PyErr_Clear();
st->get_annotate_from_class_namespace = NULL;
}
else {
return NULL;
}
}
else {
st->get_annotate_from_class_namespace = PyObject_GetAttrString(
temp_module, "get_annotate_from_class_namespace"
);
Py_DECREF(temp_module);
if (st->get_annotate_from_class_namespace == NULL) return NULL;
}

/* Initialize cached constant strings */
#define CACHED_STRING(attr, str) \
if ((st->attr = PyUnicode_InternFromString(str)) == NULL) return NULL
Expand Down
16 changes: 12 additions & 4 deletions msgspec/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# type: ignore
import collections
import inspect
import sys
import typing

Expand Down Expand Up @@ -71,6 +72,13 @@ def _eval_type(t, globalns, localns):
_eval_type = typing._eval_type


if sys.version_info >= (3, 10):
from inspect import get_annotations as _get_class_annotations
else:
def _get_class_annotations(cls):
return cls.__dict__.get("__annotations__", {})


def _apply_params(obj, mapping):
if isinstance(obj, typing.TypeVar):
return mapping.get(obj, obj)
Expand Down Expand Up @@ -149,17 +157,17 @@ 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__", {})
ann = _get_class_annotations(cls)
for name, value in ann.items():
if name in hints:
continue
if value is None:
value = type(None)
elif isinstance(value, str):
if isinstance(value, str):
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:
Copy link
Author

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.

value = type(None)
hints[name] = value
return hints

Expand Down