Skip to content

Commit 1fce989

Browse files
speediedanmauvilsa
andauthored
Allow discard_init_args_on_class_path_change to handle more nested contexts (#248)
* allow `discard_init_args_on_class_path_change` to handle more nested contexts * Further extend contexts handled by ``discard_init_args_on_class_path_change`` Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> * update changelog --------- Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com>
1 parent 14b4842 commit 1fce989

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ The semantic versioning only considers the public API as described in
1212
paths are considered internals and can change in minor and patch releases.
1313

1414

15-
v4.20.1 (2023-02-??)
15+
v4.20.1 (2023-03-??)
1616
--------------------
1717

1818
Fixed
1919
^^^^^
20+
- Allow ``discard_init_args_on_class_path_change`` to handle more nested contexts `#247
21+
<https://github.com/omni-us/jsonargparse/issues/247>`__.
22+
2023
- Dump not working for partial callable with return instance
2124
`pytorch-lightning#15340 (comment)
2225
<https://github.com/Lightning-AI/lightning/issues/15340#issuecomment-1439203008>`__.

jsonargparse/typehints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ def discard_init_args_on_class_path_change(parser_or_action, prev_val, value):
939939
sub_add_kwargs = getattr(parser_or_action, 'sub_add_kwargs', {})
940940
parser = ActionTypeHint.get_class_parser(value['class_path'], sub_add_kwargs)
941941
del_args = {}
942+
prev_val = subclass_spec_as_namespace(prev_val)
942943
for key, val in list(prev_val.init_args.__dict__.items()):
943944
action = _find_action(parser, key)
944945
if action:

jsonargparse_tests/test_signatures.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,46 @@ def __init__(self, sub: Base = lazy_instance(Sub1)) -> None:
14271427
self.assertIsInstance(init.main.sub, Sub2)
14281428
self.assertTrue(any("discarding init_args: {'s1': 'x'}" in o for o in log.output))
14291429

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))
14301470

14311471
@dataclasses.dataclass(frozen=True)
14321472
class MyDataClassA:

0 commit comments

Comments
 (0)