Skip to content

Commit 903a340

Browse files
authored
Fix to_builtins with frozendict (msgspec#1105)
Now it follows the same rules as `dict` :) 1. Converts nested elements 2. Respects `order` keyword Closes msgspec#1104
1 parent 68f53a7 commit 903a340

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20292,6 +20292,17 @@ cleanup:;
2029220292
return out;
2029320293
}
2029420294

20295+
#if PY315_PLUS
20296+
static PyObject *
20297+
to_builtins_frozendict(ToBuiltinsState *self, PyObject *obj) {
20298+
PyObject *out = to_builtins_dict(self, obj);
20299+
if (self->builtin_types & MS_BUILTIN_FROZENDICT) {
20300+
return _PyFrozenDict_NewSteal(out);
20301+
}
20302+
return out;
20303+
}
20304+
#endif
20305+
2029520306
static PyObject *
2029620307
to_builtins_struct(ToBuiltinsState *self, PyObject *obj, bool is_key) {
2029720308
if (Py_EnterRecursiveCall(" while serializing an object")) return NULL;
@@ -20573,8 +20584,7 @@ to_builtins(ToBuiltinsState *self, PyObject *obj, bool is_key) {
2057320584
}
2057420585
#if PY315_PLUS
2057520586
else if (PyFrozenDict_Check(obj)) {
20576-
if (self->builtin_types & MS_BUILTIN_FROZENDICT) goto builtin;
20577-
return to_builtins_dict(self, obj);
20587+
return to_builtins_frozendict(self, obj);
2057820588
}
2057920589
#endif
2058020590
else if (ms_is_struct_type(type)) {

‎tests/unit/test_to_builtins.py‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ def check_non_frozen(self, val, expected):
320320
assert type(expected) is dict
321321
assert val == expected
322322

323+
def check_frozen(self, val, expected):
324+
assert type(val) is frozendict
325+
assert type(expected) is frozendict
326+
assert val == expected
327+
323328
@py315_or_later_only
324329
@pytest.mark.parametrize("subclass", [False, True])
325330
def test_frozendict(self, subclass):
@@ -331,13 +336,27 @@ class in_type(frozendict):
331336
else:
332337
in_type = frozendict
333338

334-
msg = in_type({FruitStr.BANANA: 1, "b": [FruitInt.APPLE], 3: "three"})
339+
raw = {FruitStr.BANANA: 1, "b": [FruitInt.APPLE], 3: "three"}
340+
msg = in_type(raw)
335341

336342
res = to_builtins(msg)
337343
self.check_non_frozen(res, {"banana": 1, "b": [-1], 3: "three"})
338344
assert res is not msg
339345

346+
self.check_frozen(
347+
to_builtins(msg, builtin_types=[frozendict]),
348+
frozendict({"banana": 1, "b": [-1], 3: "three"}),
349+
)
350+
self.check_non_frozen(
351+
to_builtins(raw, builtin_types=[frozendict]),
352+
{"banana": 1, "b": [-1], 3: "three"},
353+
)
354+
340355
self.check_non_frozen(to_builtins(in_type()), {})
356+
self.check_frozen(
357+
to_builtins(in_type(), builtin_types=[frozendict]),
358+
frozendict(),
359+
)
341360

342361
@py315_or_later_only
343362
def test_frozendict_builtin_types(self):
@@ -349,13 +368,22 @@ def test_frozendict_builtin_types(self):
349368
frozendict(
350369
{
351370
"test": frozendict({1: "a"}),
371+
FruitStr.BANANA: 1,
372+
"b": [FruitInt.APPLE],
352373
}
353374
),
354375
builtin_types=(frozendict,),
355376
)
356377
assert type(msg) is frozendict
357378
assert type(msg["test"]) is frozendict
358-
assert msg == frozendict({"test": frozendict({1: "a"})})
379+
assert type(list(msg.keys())[1]) is str
380+
assert msg == frozendict(
381+
{
382+
"test": frozendict({1: "a"}),
383+
"banana": 1,
384+
"b": [-1],
385+
}
386+
)
359387

360388
@py315_or_later_only
361389
def test_frozendict_str_subclass_key(self):
@@ -629,9 +657,9 @@ def test_order_invalid(self):
629657
to_builtins(1, order="bad")
630658

631659
@staticmethod
632-
def assert_eq(left, right):
660+
def assert_eq(left, right, typ=dict):
633661
assert left == right
634-
if isinstance(left, dict):
662+
if isinstance(left, typ):
635663
assert list(left) == list(right)
636664

637665
@pytest.mark.parametrize("msg", [{}, {"y": 1, "x": 2, "z": 3}])
@@ -649,6 +677,16 @@ def test_order_dict_unsortable(self):
649677
with pytest.raises(TypeError):
650678
to_builtins({"x": 1, 1: 2}, order="deterministic")
651679

680+
@py315_or_later_only
681+
@pytest.mark.parametrize("msg", [{}, {"y": 1, "x": 2, "z": 3}])
682+
@pytest.mark.parametrize("order", [None, "deterministic", "sorted"])
683+
def test_frozendict_builtin_types_sorted(self, msg, order):
684+
msg = frozendict(msg)
685+
res = to_builtins(msg, builtin_types=[frozendict], order=order)
686+
sol = frozendict(sorted(msg.items())) if order else msg
687+
self.assert_eq(res, sol)
688+
assert type(res) is frozendict
689+
652690
@pytest.mark.parametrize("typ", [set, frozenset])
653691
@pytest.mark.parametrize("order", ["deterministic", "sorted"])
654692
def test_order_set(self, typ, rand, order):

0 commit comments

Comments
 (0)