@@ -437,3 +437,89 @@ def decode(self, _obj, _obj_type: type):
437437
438438 with pytest .raises (TypeError ):
439439 print (typed_state .data .my_enum )
440+
441+
442+ @dataclass
443+ class SimpleTypes :
444+ my_int : int
445+ my_enum : MyEnum
446+ my_path : Path
447+
448+
449+ @dataclass
450+ class TypedComposite :
451+ simple_types : SimpleTypes = field (default_factory = SimpleTypes )
452+
453+
454+ @dataclass
455+ class DataclassCollections :
456+ nested_list : list [TypedComposite ] = field (default_factory = list )
457+ nested_dict : dict [str , TypedComposite ] = field (default_factory = dict )
458+
459+
460+ def test_encode_decode_supports_collections_of_nested_dataclass (state ):
461+ typed_state = TypedState (state , DataclassCollections )
462+ typed_state .data .nested_list = [
463+ TypedComposite (
464+ SimpleTypes (my_int = 1 , my_enum = MyEnum .A , my_path = Path ("/path/to/1" ))
465+ ),
466+ TypedComposite (
467+ SimpleTypes (my_int = 2 , my_enum = MyEnum .B , my_path = Path ("/path/to/2" ))
468+ ),
469+ ]
470+
471+ typed_state .data .nested_dict = {
472+ "3" : TypedComposite (
473+ SimpleTypes (my_int = 3 , my_enum = MyEnum .C , my_path = Path ("/path/to/3" ))
474+ ),
475+ "4" : TypedComposite (
476+ SimpleTypes (my_int = 4 , my_enum = MyEnum .A , my_path = Path ("/path/to/4" ))
477+ ),
478+ }
479+
480+ assert typed_state .data .nested_list [0 ] == TypedComposite (
481+ SimpleTypes (my_int = 1 , my_enum = MyEnum .A , my_path = Path ("/path/to/1" ))
482+ )
483+ assert typed_state .data .nested_list [1 ] == TypedComposite (
484+ SimpleTypes (my_int = 2 , my_enum = MyEnum .B , my_path = Path ("/path/to/2" ))
485+ )
486+ assert typed_state .data .nested_dict ["3" ] == TypedComposite (
487+ SimpleTypes (my_int = 3 , my_enum = MyEnum .C , my_path = Path ("/path/to/3" ))
488+ )
489+ assert typed_state .data .nested_dict ["4" ] == TypedComposite (
490+ SimpleTypes (my_int = 4 , my_enum = MyEnum .A , my_path = Path ("/path/to/4" ))
491+ )
492+
493+ assert state [typed_state .name .nested_list ] == [
494+ {
495+ "simple_types" : {
496+ "my_int" : 1 ,
497+ "my_enum" : typed_state .encode (MyEnum .A ),
498+ "my_path" : typed_state .encode ("/path/to/1" ),
499+ }
500+ },
501+ {
502+ "simple_types" : {
503+ "my_int" : 2 ,
504+ "my_enum" : typed_state .encode (MyEnum .B ),
505+ "my_path" : typed_state .encode ("/path/to/2" ),
506+ }
507+ },
508+ ]
509+
510+ assert state [typed_state .name .nested_dict ] == {
511+ "3" : {
512+ "simple_types" : {
513+ "my_int" : 3 ,
514+ "my_enum" : typed_state .encode (MyEnum .C ),
515+ "my_path" : typed_state .encode ("/path/to/3" ),
516+ }
517+ },
518+ "4" : {
519+ "simple_types" : {
520+ "my_int" : 4 ,
521+ "my_enum" : typed_state .encode (MyEnum .A ),
522+ "my_path" : typed_state .encode ("/path/to/4" ),
523+ }
524+ },
525+ }
0 commit comments