Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support str subclasses as dict keys in encode/to_builtins #454

Merged
merged 1 commit into from
Jun 24, 2023
Merged
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
33 changes: 31 additions & 2 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -10174,6 +10174,7 @@ enum mpack_code {
};

static int mpack_encode_inline(EncoderState *self, PyObject *obj);
static int mpack_encode_dict_key_inline(EncoderState *self, PyObject *obj);
static int mpack_encode(EncoderState *self, PyObject *obj);

static int
Expand Down Expand Up @@ -10532,7 +10533,7 @@ mpack_encode_dict(EncoderState *self, PyObject *obj)
if (len == 0) return 0;
if (Py_EnterRecursiveCall(" while serializing an object")) return -1;
while (PyDict_Next(obj, &pos, &key, &val)) {
if (mpack_encode_inline(self, key) < 0) goto error;
if (mpack_encode_dict_key_inline(self, key) < 0) goto error;
if (mpack_encode_inline(self, val) < 0) goto error;
}
status = 0;
Expand Down Expand Up @@ -11023,6 +11024,31 @@ mpack_encode_inline(EncoderState *self, PyObject *obj)
}
}

static MS_INLINE int
mpack_encode_dict_key_inline(EncoderState *self, PyObject *obj)
{
PyTypeObject *type = Py_TYPE(obj);

if (PyUnicode_Check(obj)) {
return mpack_encode_str(self, obj);
}
else if (type == &PyLong_Type) {
return mpack_encode_long(self, obj);
}
else if (type == &PyFloat_Type) {
return mpack_encode_float(self, obj);
}
else if (PyList_Check(obj)) {
return mpack_encode_list(self, obj);
}
else if (PyDict_Check(obj)) {
return mpack_encode_dict(self, obj);
}
else {
return mpack_encode_uncommon(self, type, obj);
}
}

static int
mpack_encode(EncoderState *self, PyObject *obj) {
return mpack_encode_inline(self, obj);
Expand Down Expand Up @@ -11555,7 +11581,7 @@ json_encode_dict(EncoderState *self, PyObject *obj)
if (ms_write(self, "{", 1) < 0) return -1;
if (Py_EnterRecursiveCall(" while serializing an object")) return -1;
while (PyDict_Next(obj, &pos, &key, &val)) {
if (MS_LIKELY(PyUnicode_CheckExact(key))) {
if (MS_LIKELY(PyUnicode_Check(key))) {
if (json_encode_str(self, key) < 0) goto cleanup;
}
else {
Expand Down Expand Up @@ -17460,6 +17486,9 @@ to_builtins(ToBuiltinsState *self, PyObject *obj, bool is_key) {
else if (Py_TYPE(type) == self->mod->EnumMetaType) {
return to_builtins_enum(self, obj);
}
else if (is_key & PyUnicode_Check(obj)) {
return PyObject_Str(obj);
}
else if (PyType_IsSubtype(type, (PyTypeObject *)(self->mod->UUIDType))) {
if (self->builtin_types & MS_BUILTIN_UUID) goto builtin;
return to_builtins_uuid(self, obj);
Expand Down
7 changes: 7 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,13 @@ def test_decode_dict_int_literal_key(self):
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 3"):
dec.decode(b'{"-1": 10, "3": 20}')

def test_encode_dict_str_subclass_key(self):
class mystr(str):
pass

msg = msgspec.json.encode({mystr("test"): 1})
assert msg == b'{"test":1}'

@pytest.mark.parametrize(
"s, error",
[
Expand Down
8 changes: 8 additions & 0 deletions tests/test_msgpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,14 @@ def test_dict_any_key(self):
):
dec.decode(enc.encode({1: 2}))

def test_dict_str_subclass_key(self):
class mystr(str):
pass

msg1 = msgspec.msgpack.encode({mystr("test"): 1})
msg2 = msgspec.msgpack.encode({"test": 1})
assert msg1 == msg2

def test_dict_typed(self):
enc = msgspec.msgpack.Encoder()
dec = msgspec.msgpack.Decoder(Dict[str, int])
Expand Down
8 changes: 8 additions & 0 deletions tests/test_to_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ class in_type(dict):
res = to_builtins(in_type())
assert res == {}

def test_dict_str_subclass_key(self):
class mystr(str):
pass

msg = to_builtins({mystr("test"): 1})
assert msg == {"test": 1}
assert type(list(msg.keys())[0]) is str

def test_dict_unsupported_key(self):
msg = {Bad(): 1}
with pytest.raises(TypeError, match="Encoding objects of type Bad"):
Expand Down