Skip to content

Commit 0d60546

Browse files
authored
add_argument of a positional and providing default now raises an error (#694)
1 parent c1f8d8f commit 0d60546

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Changed
2525
- ``validate`` now checks values before required so that errors related to wrong
2626
level in a config are easier to understand (`#681
2727
<https://github.com/omni-us/jsonargparse/pull/681>`__).
28+
- ``add_argument`` of a positional and providing ``default`` now raises an error
29+
(`#694 <https://github.com/omni-us/jsonargparse/pull/694>`__).
2830

2931
Fixed
3032
^^^^^

jsonargparse/_core.py

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def add_argument(self, *args, enable_path: bool = False, **kwargs):
147147
ActionJsonnet._check_ext_vars_action(parser, action)
148148
if is_meta_key(action.dest):
149149
raise ValueError(f'Argument with destination name "{action.dest}" not allowed.')
150+
if action.option_strings == [] and "default" in kwargs:
151+
raise ValueError("Positional arguments not allowed to have a default value.")
150152
if action.help is None:
151153
action.help = empty_help
152154
if action.required:

jsonargparse_tests/test_core.py

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
)
4848

4949

50+
def test_add_argument_positional_with_default(parser):
51+
with pytest.raises(ValueError, match="Positional arguments not allowed to have a default value"):
52+
parser.add_argument("pos", default="abc")
53+
54+
5055
def test_parse_args_simple(parser):
5156
parser.add_argument("--op", type=int)
5257
assert parser.parse_args(["--op=1"]) == Namespace(op=1)

0 commit comments

Comments
 (0)