Skip to content

Commit 8b95fd2

Browse files
authored
Fix add_class_arguments with dashes in the nested_key fail to instantiate (#679)
1 parent d69f7eb commit 8b95fd2

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.rst

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ 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.37.1 (2025-02-??)
16+
--------------------
17+
18+
Fixed
19+
^^^^^
20+
- ``add_class_arguments`` with dashes in the ``nested_key`` fail to instantiate
21+
(`#679 <https://github.com/omni-us/jsonargparse/pull/679>`__).
22+
23+
1524
v4.37.0 (2025-02-14)
1625
--------------------
1726

jsonargparse/_signatures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def _create_group_if_requested(
546546
if config_load and nested_key is not None:
547547
group.add_argument("--" + nested_key, action=_ActionConfigLoad(basetype=config_load_type))
548548
if inspect.isclass(obj) and nested_key is not None and instantiate:
549-
group.dest = nested_key
549+
group.dest = nested_key.replace("-", "_")
550550
group.group_class = obj
551551
group.instantiate_class = group_instantiate_class
552552
return group

jsonargparse_tests/test_signatures.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,18 @@ def test_add_class_implemented_with_new(parser):
243243

244244
class RequiredParams:
245245
def __init__(self, n: int, m: float):
246-
pass
246+
self.n = n
247+
self.m = m
248+
249+
250+
def test_add_class_group_name_dash_required_parameters(parser):
251+
parser.add_class_arguments(RequiredParams, "required-params")
252+
assert "required-params" in parser.groups
253+
cfg = parser.parse_args(["--required-params.n=6", "--required-params.m=0.9"])
254+
init = parser.instantiate_classes(cfg)
255+
assert isinstance(init.required_params, RequiredParams)
256+
assert init.required_params.n == 6
257+
assert init.required_params.m == 0.9
247258

248259

249260
def test_add_class_with_required_parameters(parser):

0 commit comments

Comments
 (0)