@@ -1427,6 +1427,46 @@ def __init__(self, sub: Base = lazy_instance(Sub1)) -> None:
1427
1427
self .assertIsInstance (init .main .sub , Sub2 )
1428
1428
self .assertTrue (any ("discarding init_args: {'s1': 'x'}" in o for o in log .output ))
1429
1429
1430
+ def test_config_nested_dict_discard_init_args (self ):
1431
+ class Base :
1432
+ def __init__ (self , b : float = 0.5 ):
1433
+ pass
1434
+
1435
+ class Sub1 (Base ):
1436
+ def __init__ (self , s1 : int = 3 , ** kwargs ):
1437
+ super ().__init__ (** kwargs )
1438
+
1439
+ class Sub2 (Base ):
1440
+ def __init__ (self , s2 : int = 4 , ** kwargs ):
1441
+ super ().__init__ (** kwargs )
1442
+
1443
+ class Main :
1444
+ def __init__ (self , sub : Optional [Dict ] = None ) -> None :
1445
+ self .sub = sub
1446
+
1447
+ configs , subconfigs , config_paths = {}, {}, {}
1448
+ with mock_module (Base , Sub1 , Sub2 , Main ) as module :
1449
+ parser = ArgumentParser (exit_on_error = False , logger = {'level' : 'DEBUG' })
1450
+ parser .add_argument ('--config' , action = ActionConfigFile )
1451
+ parser .add_subclass_arguments (Main , 'main' )
1452
+ parser .set_defaults (main = lazy_instance (Main ))
1453
+ for c in [1 , 2 ]:
1454
+ subconfigs [c ] = {
1455
+ 'sub' : {
1456
+ 'class_path' : f'{ module } .Sub{ c } ' ,
1457
+ 'init_args' : {f's{ c } ' : c },
1458
+ }
1459
+ }
1460
+ configs [c ] = {'main' : {'class_path' : f'{ module } .Main' ,'init_args' : subconfigs [c ],}}
1461
+ config_paths [c ] = Path (f'config{ c } .yaml' )
1462
+ config_paths [c ].write_text (yaml .safe_dump (configs [c ]))
1463
+
1464
+ with self .assertLogs (logger = parser .logger , level = 'DEBUG' ) as log :
1465
+ cfg = parser .parse_args ([f'--config={ config_paths [1 ]} ' , f'--config={ config_paths [2 ]} ' ])
1466
+ init = parser .instantiate_classes (cfg )
1467
+ self .assertIsInstance (init .main , Main )
1468
+ self .assertTrue (init .main .sub ['init_args' ]['s2' ], 2 )
1469
+ self .assertTrue (any ("discarding init_args: {'s1': 1}" in o for o in log .output ))
1430
1470
1431
1471
@dataclasses .dataclass (frozen = True )
1432
1472
class MyDataClassA :
0 commit comments