Repository: omni-us/jsonargparse
Pull request #679 (2025-02-18 06:34:41)

Fix add_class_arguments with dashes in the nested_key fail to instantiate
## What does this PR do?

Fixes https://github.com/omni-us/jsonargparse/issues/301#issuecomment-2650949459

## Before submitting

- [x] Did you read the [contributing guideline](https://github.com/omni-us/jsonargparse/blob/main/CONTRIBUTING.rst)?
- [n/a] Did you update **the documentation**? (readme and public docstrings)
- [x] Did you write **unit tests** such that there is 100% coverage on related code? (required for bug fixes and new features)
- [x] Did you verify that new and existing **tests pass locally**?
- [x] Did you make sure that all changes preserve **backward compatibility**?
- [x] Did you update **the CHANGELOG**? (not for typos, docs, test updates, or minor internal changes/refactors)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/omni-us/jsonargparse/pull/679?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`d69f7eb`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/d69f7eb367460d85447d93d9ac3602d760651125?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) to head [(`690ee85`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/690ee856a22785707bb16b8a8c77775d4d85420b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main      #679   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         6686      6686           
=========================================
  Hits          6686      6686           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/omni-us/jsonargparse/pull/679?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).

sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=omni-us_jsonargparse&pullRequest=679) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=679&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=679&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=omni-us_jsonargparse&pullRequest=679&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=omni-us_jsonargparse&pullRequest=679&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=omni-us_jsonargparse&pullRequest=679&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=omni-us_jsonargparse&pullRequest=679)



Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 853f2b2..967aba7 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -12,6 +12,15 @@ The semantic versioning only considers the public API as described in
 paths are considered internals and can change in minor and patch releases.
 
 
+v4.37.1 (2025-02-??)
+--------------------
+
+Fixed
+^^^^^
+- ``add_class_arguments`` with dashes in the ``nested_key`` fail to instantiate
+  (`#679 <https://github.com/omni-us/jsonargparse/pull/679>`__).
+
+
 v4.37.0 (2025-02-14)
 --------------------
 
diff --git a/jsonargparse/_signatures.py b/jsonargparse/_signatures.py
index 48d27e7..382073d 100644
--- a/jsonargparse/_signatures.py
+++ b/jsonargparse/_signatures.py
@@ -546,7 +546,7 @@ class SignatureArguments(LoggerProperty):
             if config_load and nested_key is not None:
                 group.add_argument("--" + nested_key, action=_ActionConfigLoad(basetype=config_load_type))
             if inspect.isclass(obj) and nested_key is not None and instantiate:
-                group.dest = nested_key
+                group.dest = nested_key.replace("-", "_")
                 group.group_class = obj
                 group.instantiate_class = group_instantiate_class
         return group


Test patch:
diff --git a/jsonargparse_tests/test_signatures.py b/jsonargparse_tests/test_signatures.py
index aa39a8d..6645a21 100644
--- a/jsonargparse_tests/test_signatures.py
+++ b/jsonargparse_tests/test_signatures.py
@@ -243,7 +243,18 @@ def test_add_class_implemented_with_new(parser):
 
 class RequiredParams:
     def __init__(self, n: int, m: float):
-        pass
+        self.n = n
+        self.m = m
+
+
+def test_add_class_group_name_dash_required_parameters(parser):
+    parser.add_class_arguments(RequiredParams, "required-params")
+    assert "required-params" in parser.groups
+    cfg = parser.parse_args(["--required-params.n=6", "--required-params.m=0.9"])
+    init = parser.instantiate_classes(cfg)
+    assert isinstance(init.required_params, RequiredParams)
+    assert init.required_params.n == 6
+    assert init.required_params.m == 0.9
 
 
 def test_add_class_with_required_parameters(parser):