Skip to content

Commit 8ac5ec1

Browse files
committed
Python 3.14: Call __annotate__ on type objects to get annotations
Fixes msgspec#795
1 parent bc60e96 commit 8ac5ec1

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎msgspec/_core.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5817,7 +5817,15 @@ structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly)
58175817
PyObject *annotations = PyDict_GetItemString(
58185818
info->namespace, "__annotations__"
58195819
);
5820-
if (annotations == NULL) return 0;
5820+
if (annotations == NULL) {
5821+
PyObject *annotate = PyDict_GetItemString(
5822+
info->namespace, "__annotate__"
5823+
);
5824+
if (annotate == NULL) return 0;
5825+
PyObject *format = PyLong_FromLong(1); /* annotationlib.Format.VALUE */
5826+
annotations = PyObject_CallOneArg(annotate, format);
5827+
Py_XDECREF(format);
5828+
}
58215829

58225830
if (!PyDict_Check(annotations)) {
58235831
PyErr_SetString(PyExc_TypeError, "__annotations__ must be a dict");

‎msgspec/_utils.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ def get_class_annotations(obj):
149149
cls_locals = dict(vars(cls))
150150
cls_globals = getattr(sys.modules.get(cls.__module__, None), "__dict__", {})
151151

152-
ann = cls.__dict__.get("__annotations__", {})
152+
if "__annotations__" in cls.__dict__:
153+
ann = cls.__dict__["__annotations__"]
154+
elif "__annotate__" in cls.__dict__:
155+
ann = cls.__dict__["__annotate__"](1) # annotationlib.Format.VALUE
156+
else:
157+
ann = {}
153158
for name, value in ann.items():
154159
if name in hints:
155160
continue

0 commit comments

Comments
 (0)