Skip to content

Commit a7f4bc4

Browse files
JelleZijlstrakumaraditya303
authored andcommitted
Fix annotations support on 3.14
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 msgspec#810. Fixes msgspec#651. Fixes msgspec#795.
1 parent 478f1ef commit a7f4bc4

2 files changed

Lines changed: 69 additions & 6 deletions

File tree

‎msgspec/_core.c‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ typedef struct {
481481
#endif
482482
PyObject *astimezone;
483483
PyObject *re_compile;
484+
PyObject *get_annotate_from_class_namespace;
484485
uint8_t gc_cycle;
485486
} MsgspecState;
486487

@@ -5827,12 +5828,45 @@ structmeta_is_classvar(
58275828

58285829
static int
58295830
structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly) {
5830-
PyObject *annotations = PyDict_GetItemString(
5831+
PyObject *annotations = PyDict_GetItemString( // borrowed reference
58315832
info->namespace, "__annotations__"
58325833
);
5833-
if (annotations == NULL) return 0;
5834+
if (annotations == NULL) {
5835+
if (mod->get_annotate_from_class_namespace != NULL) {
5836+
PyObject *annotate = PyObject_CallOneArg(
5837+
mod->get_annotate_from_class_namespace, info->namespace
5838+
);
5839+
if (annotate == NULL) {
5840+
return -1;
5841+
}
5842+
if (annotate == Py_None) {
5843+
Py_DECREF(annotate);
5844+
return 0;
5845+
}
5846+
PyObject *format = PyLong_FromLong(1); /* annotationlib.Format.VALUE */
5847+
if (format == NULL) {
5848+
Py_DECREF(annotate);
5849+
return -1;
5850+
}
5851+
annotations = PyObject_CallOneArg(
5852+
annotate, format
5853+
);
5854+
Py_DECREF(annotate);
5855+
Py_DECREF(format);
5856+
if (annotations == NULL) {
5857+
return -1;
5858+
}
5859+
}
5860+
else {
5861+
return 0; // No annotations, nothing to do
5862+
}
5863+
}
5864+
else {
5865+
Py_INCREF(annotations);
5866+
}
58345867

58355868
if (!PyDict_Check(annotations)) {
5869+
Py_DECREF(annotations);
58365870
PyErr_SetString(PyExc_TypeError, "__annotations__ must be a dict");
58375871
return -1;
58385872
}
@@ -5882,6 +5916,7 @@ structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly)
58825916
}
58835917
return 0;
58845918
error:
5919+
Py_DECREF(annotations);
58855920
Py_XDECREF(module_ns);
58865921
return -1;
58875922
}
@@ -22238,6 +22273,26 @@ PyInit__core(void)
2223822273
Py_DECREF(temp_module);
2223922274
if (st->re_compile == NULL) return NULL;
2224022275

22276+
/* annotationlib.get_annotate_from_class_namespace */
22277+
temp_module = PyImport_ImportModule("annotationlib");
22278+
if (temp_module == NULL) {
22279+
if (PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)) {
22280+
// Below Python 3.14
22281+
PyErr_Clear();
22282+
st->get_annotate_from_class_namespace = NULL;
22283+
}
22284+
else {
22285+
return NULL;
22286+
}
22287+
}
22288+
else {
22289+
st->get_annotate_from_class_namespace = PyObject_GetAttrString(
22290+
temp_module, "get_annotate_from_class_namespace"
22291+
);
22292+
Py_DECREF(temp_module);
22293+
if (st->get_annotate_from_class_namespace == NULL) return NULL;
22294+
}
22295+
2224122296
/* Initialize cached constant strings */
2224222297
#define CACHED_STRING(attr, str) \
2224322298
if ((st->attr = PyUnicode_InternFromString(str)) == NULL) return NULL

‎msgspec/_utils.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# type: ignore
22
import collections
3+
import inspect
34
import sys
45
import typing
56

@@ -71,6 +72,13 @@ def _eval_type(t, globalns, localns):
7172
_eval_type = typing._eval_type
7273

7374

75+
if sys.version_info >= (3, 10):
76+
from inspect import get_annotations as _get_class_annotations
77+
else:
78+
def _get_class_annotations(cls):
79+
return cls.__dict__.get("__annotations__", {})
80+
81+
7482
def _apply_params(obj, mapping):
7583
if isinstance(obj, typing.TypeVar):
7684
return mapping.get(obj, obj)
@@ -149,17 +157,17 @@ def get_class_annotations(obj):
149157
cls_locals = dict(vars(cls))
150158
cls_globals = getattr(sys.modules.get(cls.__module__, None), "__dict__", {})
151159

152-
ann = cls.__dict__.get("__annotations__", {})
160+
ann = _get_class_annotations(cls)
153161
for name, value in ann.items():
154162
if name in hints:
155163
continue
156-
if value is None:
157-
value = type(None)
158-
elif isinstance(value, str):
164+
if isinstance(value, str):
159165
value = _forward_ref(value)
160166
value = _eval_type(value, cls_locals, cls_globals)
161167
if mapping is not None:
162168
value = _apply_params(value, mapping)
169+
if value is None:
170+
value = type(None)
163171
hints[name] = value
164172
return hints
165173

0 commit comments

Comments
 (0)