File tree 2 files changed +29
-1
lines changed
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -960,7 +960,7 @@ def _update_from_parsed(
960
960
filled [key ] = value
961
961
if key not in final :
962
962
final [key ] = value
963
- if isinstance (value , dict ):
963
+ if isinstance (value , dict ) and isinstance ( final [ key ], dict ) :
964
964
filled [key ], final [key ] = cls ._update_from_parsed (
965
965
value , filled [key ], final [key ]
966
966
)
Original file line number Diff line number Diff line change @@ -1431,3 +1431,31 @@ def test_parse_strings_interpretable_as_ints():
1431
1431
)
1432
1432
assert cfg ["a" ]["foo" ] == [3 , "003" , "y" ]
1433
1433
assert cfg ["b" ]["bar" ] == 3
1434
+
1435
+
1436
+ def test_dict_casting ():
1437
+ class CastStrAsDict :
1438
+ @classmethod
1439
+ def validate (cls , value ):
1440
+ if isinstance (value , str ):
1441
+ return {value : True }
1442
+ return value
1443
+
1444
+ @classmethod
1445
+ def __get_validators__ (cls ):
1446
+ yield cls .validate
1447
+
1448
+ class SectionSchema (BaseModel ):
1449
+ key : CastStrAsDict
1450
+
1451
+ class MainSchema (BaseModel ):
1452
+ section : SectionSchema
1453
+
1454
+ cfg = Config ().from_str (
1455
+ """\
1456
+ [section]
1457
+ key = "ok"
1458
+ """
1459
+ )
1460
+
1461
+ assert my_registry .fill (cfg , schema = MainSchema ) == {"section" : {"key" : "ok" }}
You can’t perform that action at this time.
0 commit comments