Skip to content

Commit

Permalink
Fix confusing error message when adding signature parameters that con…
Browse files Browse the repository at this point in the history
…flict with existing arguments.
  • Loading branch information
mauvilsa committed Nov 20, 2023
1 parent 0e37aca commit d1a6fcd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.27.1 (2023-11-??)
--------------------

Fixed
^^^^^
- Confusing error message when adding signature parameters that conflict with
existing arguments.


v4.27.0 (2023-11-02)
--------------------

Expand Down
8 changes: 8 additions & 0 deletions jsonargparse/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ def _add_signature_arguments(
f"Skipping parameters {names} because {skip_positionals[0]} positionals requested to be skipped."
)

prefix = "--" + (nested_key + "." if nested_key else "")
for param in params:
if prefix + param.name in self._option_string_actions: # type: ignore
raise ValueError(
f"Unable to add parameter '{param.name}' from {function_or_class} because "
f"argument '{prefix + param.name}' already exists."
)

## Create group if requested ##
doc_group = get_doc_short_description(function_or_class, method_name, logger=self.logger)
component = getattr(function_or_class, method_name) if method_name else function_or_class
Expand Down
11 changes: 11 additions & 0 deletions jsonargparse_tests/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,14 @@ def test_add_function_group_config_within_config(parser, tmp_cwd):
cfg = parser.parse_args([f"--cfg={cfg_path}"])
assert str(cfg.func.__path__) == str(subcfg_path)
assert strip_meta(cfg.func) == Namespace(a1="one", a2=2.0, a3=True)


def func_param_conflict(p1: int, cfg: dict):
pass


def test_add_function_param_conflict(parser):
parser.add_argument("--cfg", action=ActionConfigFile)
with pytest.raises(ValueError) as ctx:
parser.add_function_arguments(func_param_conflict)
ctx.match("Unable to add parameter 'cfg' from")

0 comments on commit d1a6fcd

Please sign in to comment.