Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Fixed
- While parsing, internally preserve ``class_path`` for subclass disabled types
to correctly support defaults from union of dataclasses (`#921
<https://github.com/omni-us/jsonargparse/pull/921>`__).
- Misleading error message for ``parse_optionals_as_positionals=True`` and
unrecognized non-positional arguments (`#922
<https://github.com/omni-us/jsonargparse/pull/922>`__).

Changed
^^^^^^^
Expand Down
11 changes: 9 additions & 2 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,25 @@

return namespace, args

def _positional_optionals(self, cfg, unk):

Check failure on line 311 in jsonargparse/_core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&issues=AZ7toSaY74NFkRFeEedH&open=AZ7toSaY74NFkRFeEedH&pullRequest=922
if len(unk) == 0 or not supports_optionals_as_positionals(self):
return cfg, unk

for action in get_optionals_as_positionals_actions(self, include_positionals=True):
if action.option_strings == []:
if cfg.get(action.dest) is None:
if cfg.get(action.dest) is get_parsing_setting("unset_sentinel"):
self._logger.debug(f"Positional argument {action.dest} missing, aborting _positional_optionals")
break
continue

cfg[action.dest] = self._check_value_key(action, unk.pop(0), action.dest, cfg)
value = unk.pop(0)
try:
cfg[action.dest] = self._check_value_key(action, value, action.dest, cfg)
except (TypeError, ValueError) as ex:
if isinstance(value, str) and value.startswith("--"):
raise argument_error(f"unrecognized arguments: {' '.join([value] + unk)}") from ex
raise

if len(unk) == 0:
break

Expand Down
10 changes: 10 additions & 0 deletions jsonargparse_tests/test_parsing_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ def test_parse_optionals_as_positionals_simple(parser, logger, subtests):
parser.parse_args(["--unk=x"])
assert "Positional argument p1 missing, aborting _positional_optionals" in logs.getvalue()

with subtests.test("mixed unrecognized"):
with capture_logs(logger) as logs:
with pytest.raises(ArgumentError, match="unrecognized arguments: --unexpected arg xyz"):
parser.parse_args(["p1", "3", "--unexpected", "arg", "xyz"])
assert "unrecognized arguments: --unexpected arg xyz" in logs.getvalue()


def test_parse_optionals_as_positionals_subcommands(parser, subparser, subtests):
set_parsing_settings(parse_optionals_as_positionals=True)
Expand Down Expand Up @@ -153,6 +159,10 @@ def test_parse_optionals_as_positionals_subcommands(parser, subparser, subtests)
parser.parse_args(["subcmd", "p1", "o2", "5"])
assert re.match('Parser key "o1".*Given value: o2', ex.value.message, re.DOTALL)

with subtests.test("mixed unrecognized"):
with pytest.raises(ArgumentError, match="unrecognized arguments: --unexpected=arg"):
parser.parse_args(["subcmd", "p1", "3", "--unexpected=arg"])


def test_optionals_as_positionals_usage_wrap(parser):
set_parsing_settings(parse_optionals_as_positionals=True)
Expand Down