Skip to content

Commit

Permalink
Support Raw in convert
Browse files Browse the repository at this point in the history
Previously `Raw` inputs weren't supported in `convert`, now they are.
  • Loading branch information
jcrist committed Oct 20, 2024
1 parent c80616a commit 74ebfe0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -20560,6 +20560,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,
Expand Down Expand Up @@ -21670,6 +21681,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);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 74ebfe0

Please sign in to comment.