Skip to content
Closed
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
10 changes: 9 additions & 1 deletion msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5817,7 +5817,15 @@ structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly)
PyObject *annotations = PyDict_GetItemString(
info->namespace, "__annotations__"
);
if (annotations == NULL) return 0;
if (annotations == NULL) {
PyObject *annotate = PyDict_GetItemString(
info->namespace, "__annotate__"
);
if (annotate == NULL) return 0;
PyObject *format = PyLong_FromLong(1); /* annotationlib.Format.VALUE */
annotations = PyObject_CallOneArg(annotate, format);
Py_XDECREF(format);
}

if (!PyDict_Check(annotations)) {
PyErr_SetString(PyExc_TypeError, "__annotations__ must be a dict");
Expand Down
7 changes: 6 additions & 1 deletion msgspec/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ 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__", {})

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.

if "__annotations__" in cls.__dict__:
ann = cls.__dict__["__annotations__"]
elif "__annotate__" in cls.__dict__:
ann = cls.__dict__["__annotate__"](1) # annotationlib.Format.VALUE
else:
ann = {}
for name, value in ann.items():
if name in hints:
continue
Expand Down