From ef698544fb05efd321a1bffcc5ea014085c37bc1 Mon Sep 17 00:00:00 2001 From: Jim Crist-Harif Date: Sun, 20 Oct 2024 14:13:08 -0500 Subject: [PATCH] Support `Raw` in `convert` Previously `Raw` inputs weren't supported in `convert`, now they are. --- msgspec/_core.c | 14 ++++++++++++++ tests/test_convert.py | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/msgspec/_core.c b/msgspec/_core.c index 55f79ab..c994bce 100644 --- a/msgspec/_core.c +++ b/msgspec/_core.c @@ -20556,6 +20556,17 @@ convert_immutable( return ms_validation_error(expected, type, path); } +static PyObject * +convert_raw( + ConvertState *self, PyObject *obj, TypeNode *type, PathNode *path +) { + if (type->types == 0) { + Py_INCREF(obj); + return obj; + } + return ms_validation_error("raw", type, path); +} + static PyObject * convert_seq_to_list( ConvertState *self, PyObject **items, Py_ssize_t size, @@ -21666,6 +21677,9 @@ convert( else if (pytype == &Ext_Type) { return convert_immutable(self, MS_TYPE_EXT, "ext", obj, type, path); } + else if (pytype == &Raw_Type) { + return convert_raw(self, obj, type, path); + } else if (PyAnySet_Check(obj)) { return convert_any_set(self, obj, type, path); } diff --git a/tests/test_convert.py b/tests/test_convert.py index 0bb8cdc..ea69783 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -2600,3 +2600,14 @@ def dec_hook(typ, x): assert rec.value.__cause__ is rec.value.__context__ assert type(rec.value.__cause__) is TypeError + + +class TestRaw: + def test_raw(self): + raw = msgspec.Raw(b"123") + + class Ex(Struct): + x: msgspec.Raw + + sol = Ex(x=raw) + assert convert({"x": raw}, type=Ex) == sol