Skip to content

Commit 7e2d414

Browse files
[3.12] gh-72795: Make positional arguments with nargs='*' or REMAINDER non-required (GH-124306) (GH-124422)
This allows to use positional argument with nargs='*' and without default in mutually exclusive group and improves error message about required arguments. (cherry picked from commit 3c83f99) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 0e838b5 commit 7e2d414

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

Lib/argparse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,9 +1575,8 @@ def _get_positional_kwargs(self, dest, **kwargs):
15751575

15761576
# mark positional arguments as required if at least one is
15771577
# always required
1578-
if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
1579-
kwargs['required'] = True
1580-
if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
1578+
nargs = kwargs.get('nargs')
1579+
if nargs not in [OPTIONAL, ZERO_OR_MORE, REMAINDER, SUPPRESS, 0]:
15811580
kwargs['required'] = True
15821581

15831582
# return the keyword arguments with no option strings

Lib/test/test_argparse.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ def get_parser(self, required):
30333033
group = parser.add_mutually_exclusive_group(required=required)
30343034
group.add_argument('--foo', action='store_true', help='FOO')
30353035
group.add_argument('--spam', help='SPAM')
3036-
group.add_argument('badger', nargs='*', default='X', help='BADGER')
3036+
group.add_argument('badger', nargs='*', help='BADGER')
30373037
return parser
30383038

30393039
failures = [
@@ -3044,13 +3044,13 @@ def get_parser(self, required):
30443044
'--foo X Y',
30453045
]
30463046
successes = [
3047-
('--foo', NS(foo=True, spam=None, badger='X')),
3048-
('--spam S', NS(foo=False, spam='S', badger='X')),
3047+
('--foo', NS(foo=True, spam=None, badger=[])),
3048+
('--spam S', NS(foo=False, spam='S', badger=[])),
30493049
('X', NS(foo=False, spam=None, badger=['X'])),
30503050
('X Y Z', NS(foo=False, spam=None, badger=['X', 'Y', 'Z'])),
30513051
]
30523052
successes_when_not_required = [
3053-
('', NS(foo=False, spam=None, badger='X')),
3053+
('', NS(foo=False, spam=None, badger=[])),
30543054
]
30553055

30563056
usage_when_not_required = '''\
@@ -6020,7 +6020,28 @@ def test_required_args(self):
60206020
self.parser.add_argument('bar')
60216021
self.parser.add_argument('baz')
60226022
self.assertRaisesRegex(argparse.ArgumentError,
6023-
'the following arguments are required: bar, baz',
6023+
'the following arguments are required: bar, baz$',
6024+
self.parser.parse_args, [])
6025+
6026+
def test_required_args_optional(self):
6027+
self.parser.add_argument('bar')
6028+
self.parser.add_argument('baz', nargs='?')
6029+
self.assertRaisesRegex(argparse.ArgumentError,
6030+
'the following arguments are required: bar$',
6031+
self.parser.parse_args, [])
6032+
6033+
def test_required_args_zero_or_more(self):
6034+
self.parser.add_argument('bar')
6035+
self.parser.add_argument('baz', nargs='*')
6036+
self.assertRaisesRegex(argparse.ArgumentError,
6037+
'the following arguments are required: bar$',
6038+
self.parser.parse_args, [])
6039+
6040+
def test_required_args_remainder(self):
6041+
self.parser.add_argument('bar')
6042+
self.parser.add_argument('baz', nargs='...')
6043+
self.assertRaisesRegex(argparse.ArgumentError,
6044+
'the following arguments are required: bar$',
60246045
self.parser.parse_args, [])
60256046

60266047
def test_required_mutually_exclusive_args(self):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Positional arguments with :ref:`nargs` equal to ``'*'`` or
2+
:data:`!argparse.REMAINDER` are no longer required. This allows to use
3+
positional argument with ``nargs='*'`` and without ``default`` in mutually
4+
exclusive group and improves error message about required arguments.

0 commit comments

Comments
 (0)