Skip to content

Commit 295919d

Browse files
authored
ActionParser not updating dest of groups for instantiation (#707)
1 parent e90e8e0 commit 295919d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Changed
3030
``fail_untyped=True`` (`#697
3131
<https://github.com/omni-us/jsonargparse/pull/697>`__).
3232

33+
Fixed
34+
^^^^^
35+
- ``ActionParser`` not updating ``dest`` of groups for instantiation (`#707
36+
<https://github.com/omni-us/jsonargparse/pull/707>`__).
37+
3338

3439
v4.38.0 (2025-03-26)
3540
--------------------

jsonargparse/_actions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ def add_prefix(key):
578578
base_action_group._actions = filter_default_actions(base_action_group._actions)
579579
base_action_group._group_actions = filter_default_actions(base_action_group._group_actions)
580580
extra_action_groups = subparser._action_groups[2:]
581+
for group in extra_action_groups:
582+
if group.dest is not None:
583+
group.dest = dest + "." + group.dest
581584

582585
parser.add_argument(args[0], action=_ActionConfigLoad)
583586
parser.required_args.update(required_args)

jsonargparse_tests/test_signatures.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from jsonargparse import (
11+
ActionParser,
1112
ArgumentError,
1213
Namespace,
1314
lazy_instance,
@@ -434,6 +435,36 @@ def test_add_class_unmatched_default_type(parser):
434435
assert json_or_yaml_load(dump) == {"cls": {"p1": "x", "p2": "deprecated"}}
435436

436437

438+
class LeafClass:
439+
def __init__(self, p1: int = 1, p2: str = "-"):
440+
self.p1 = p1
441+
self.p2 = p2
442+
443+
444+
def test_add_class_and_action_parser(parser, subparser):
445+
subparser.add_class_arguments(LeafClass, "deep.leaf")
446+
parser.add_argument("--nested", action=ActionParser(parser=subparser))
447+
parser.add_argument("--config", action="config")
448+
449+
config = {
450+
"nested": {
451+
"deep": {
452+
"leaf": {
453+
"p1": 2,
454+
"p2": "x",
455+
}
456+
}
457+
}
458+
}
459+
cfg = parser.parse_args([f"--config={json.dumps(config)}"])
460+
init = parser.instantiate_classes(cfg)
461+
assert list(cfg.keys()) == ["nested.deep.leaf.p1", "nested.deep.leaf.p2", "config"]
462+
assert list(init.keys()) == ["nested.deep.leaf", "config"]
463+
assert isinstance(init.nested.deep.leaf, LeafClass)
464+
assert init.nested.deep.leaf.p1 == 2
465+
assert init.nested.deep.leaf.p2 == "x"
466+
467+
437468
# add_method_arguments tests
438469

439470

0 commit comments

Comments
 (0)