From bb5bc8dc75509ec6a2e6e0f0b3359028e584c651 Mon Sep 17 00:00:00 2001 From: Daniel Buecherl Date: Wed, 18 Jun 2025 16:40:52 +0200 Subject: [PATCH 1/2] Expose class to get_tag hook --- msgspec/_core.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/msgspec/_core.c b/msgspec/_core.c index 147ad95a..d7e46303 100644 --- a/msgspec/_core.c +++ b/msgspec/_core.c @@ -6097,8 +6097,18 @@ structmeta_construct_tag(StructMetaInfo *info, MsgspecState *mod, PyObject *cls) if (PyCallable_Check(info->temp_tag)) { PyObject *qualname = simple_qualname(cls); if (qualname == NULL) return -1; - info->tag_value = PyObject_CallOneArg(info->temp_tag, qualname); - Py_DECREF(qualname); + + PyObject *temp_args = PyTuple_New(2); + if (temp_args == NULL) { + Py_DECREF(qualname); + return -1; + } + + PyTuple_SetItem(temp_args, 0, qualname); + Py_INCREF(cls); + PyTuple_SetItem(temp_args, 1, cls); + info->tag_value = PyObject_Call(info->temp_tag, temp_args, NULL); + Py_DECREF(temp_args); if (info->tag_value == NULL) return -1; } else { From 08ff1e60d1f8e6e91207e9783e4ac3e74e8a789f Mon Sep 17 00:00:00 2001 From: Daniel Buecherl Date: Thu, 19 Jun 2025 14:10:36 +0200 Subject: [PATCH 2/2] Try 2 arg call, fallback to 1 arg call --- msgspec/_core.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/msgspec/_core.c b/msgspec/_core.c index d7e46303..d62f7bc3 100644 --- a/msgspec/_core.c +++ b/msgspec/_core.c @@ -6104,10 +6104,19 @@ structmeta_construct_tag(StructMetaInfo *info, MsgspecState *mod, PyObject *cls) return -1; } - PyTuple_SetItem(temp_args, 0, qualname); Py_INCREF(cls); + PyTuple_SetItem(temp_args, 0, qualname); PyTuple_SetItem(temp_args, 1, cls); + + // Try call with two Args info->tag_value = PyObject_Call(info->temp_tag, temp_args, NULL); + + if (info->tag_value == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { + PyErr_Clear(); + // Call with one Arg + info->tag_value = PyObject_CallOneArg(info->temp_tag, qualname); + } + Py_DECREF(temp_args); if (info->tag_value == NULL) return -1; }