@@ -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