Skip to content

Commit 9e526d9

Browse files
committed
Fixed failure to instantiate nested class group without arguments (lightning#17263).
1 parent 10ef8b1 commit 9e526d9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Fixed
3737
- ``dataclass`` from pydantic not working (`#100 comment
3838
<https://github.com/omni-us/jsonargparse/issues/100#issuecomment-1408413796>`__).
3939
- ``add_dataclass_arguments`` not forwarding ``sub_configs`` parameter.
40+
- Failure to instantiate nested class group without arguments (`lightning#17263
41+
<https://github.com/Lightning-AI/lightning/issues/17263>`__).
4042

4143
Changed
4244
^^^^^^^

jsonargparse/signatures.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ def group_instantiate_class(group, cfg):
534534
value = {}
535535
parent = cfg
536536
key = group.dest
537-
assert '.' not in key
538537
parent[key] = group.group_class(**value)
539538

540539

jsonargparse_tests/test_signatures.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ def __init__(self, a0=None):
196196
self.assertEqual([], parser.add_class_arguments(NoValidArgs))
197197

198198

199+
def test_instantiate_nested_class_without_args(self):
200+
class CallbackWithArgs:
201+
def __init__(self, p1: int):
202+
self.p1 = p1
203+
204+
class CallbackWithoutArgs:
205+
pass
206+
207+
parser = ArgumentParser()
208+
parser.add_class_arguments(CallbackWithArgs, 'callbacks.first')
209+
parser.add_class_arguments(CallbackWithoutArgs, 'callbacks.second')
210+
211+
cfg = parser.parse_args(['--callbacks.first.p1=2'])
212+
self.assertEqual(cfg.callbacks.first, Namespace(p1=2))
213+
self.assertNotIn('callbacks.second', cfg)
214+
init = parser.instantiate_classes(cfg)
215+
self.assertIsInstance(init.callbacks.first, CallbackWithArgs)
216+
self.assertIsInstance(init.callbacks.second, CallbackWithoutArgs)
217+
218+
199219
def test_add_function_with_dict_int_keys_arg(self):
200220
def func(a1: Union[int, Dict[int, int]] = 1):
201221
pass

0 commit comments

Comments
 (0)