Skip to content

gh-85935: Check for nargs=0 for positional arguments in argparse #124839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
10 changes: 9 additions & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,17 @@ def add_argument(self, *args, **kwargs):
kwargs['default'] = self.argument_default

# create the action object, and add it to the parser
action_name = kwargs.get('action')
action_class = self._pop_action_class(kwargs)
if not callable(action_class):
raise ValueError('unknown action "%s"' % (action_class,))
action = action_class(**kwargs)

# raise an error if action for positional argument does not
# consume arguments
if not action.option_strings and action.nargs == 0:
raise ValueError(f'action {action_name!r} is not valid for positional arguments')

# raise an error if the action type is not callable
type_func = self._registry_get('type', action.type, action.type)
if not callable(type_func):
Expand Down Expand Up @@ -1541,7 +1547,9 @@ def _get_positional_kwargs(self, dest, **kwargs):
# mark positional arguments as required if at least one is
# always required
nargs = kwargs.get('nargs')
if nargs not in [OPTIONAL, ZERO_OR_MORE, REMAINDER, SUPPRESS, 0]:
if nargs == 0:
raise ValueError('nargs for positionals must be != 0')
if nargs not in [OPTIONAL, ZERO_OR_MORE, REMAINDER, SUPPRESS]:
kwargs['required'] = True

# return the keyword arguments with no option strings
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5350,8 +5350,11 @@ def test_no_argument_actions(self):
with self.subTest(attrs=attrs):
self.assertTypeError('-x', action=action, **attrs)
self.assertTypeError('x', action=action, **attrs)
self.assertValueError('x', action=action,
errmsg=f"action '{action}' is not valid for positional arguments")
self.assertTypeError('-x', action=action, nargs=0)
self.assertTypeError('x', action=action, nargs=0)
self.assertValueError('x', action=action, nargs=0,
errmsg='nargs for positionals must be != 0')

def test_no_argument_no_const_actions(self):
# options with zero arguments
Expand All @@ -5371,7 +5374,7 @@ def test_more_than_one_argument_actions(self):
self.assertValueError('-x', nargs=0, action=action,
errmsg=f'nargs for {action_name} actions must be != 0')
self.assertValueError('spam', nargs=0, action=action,
errmsg=f'nargs for {action_name} actions must be != 0')
errmsg='nargs for positionals must be != 0')

# const is disallowed with non-optional arguments
for nargs in [1, '*', '+']:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`argparse.ArgumentParser.add_argument` now raises an exception if
an :ref:`action` that does not consume arguments (like 'store_const' or
'store_true') or explicit ``nargs=0`` are specified for positional
arguments.
Loading