Skip to content

Commit 794740c

Browse files
gh-53834: Fix support of arguments with choices in argparse
Positional arguments with nargs equal to '?' or '*' no longer check default against choices. Optional arguments with nargs equal to '?' no longer check const against choices.
1 parent 4a5e4aa commit 794740c

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

Lib/argparse.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,19 +2485,15 @@ def _get_values(self, action, arg_strings):
24852485
value = action.default
24862486
if isinstance(value, str):
24872487
value = self._get_value(action, value)
2488-
self._check_value(action, value)
24892488

24902489
# when nargs='*' on a positional, if there were no command-line
24912490
# args, use the default if it is anything other than None
24922491
elif (not arg_strings and action.nargs == ZERO_OR_MORE and
24932492
not action.option_strings):
24942493
if action.default is not None:
24952494
value = action.default
2496-
self._check_value(action, value)
24972495
else:
2498-
# since arg_strings is always [] at this point
2499-
# there is no need to use self._check_value(action, value)
2500-
value = arg_strings
2496+
value = []
25012497

25022498
# single argument or optional argument produces a single value
25032499
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:

Lib/test/test_argparse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,9 @@ class TestOptionalsNargsOptional(ParserTestCase):
621621
Sig('-w', nargs='?'),
622622
Sig('-x', nargs='?', const=42),
623623
Sig('-y', nargs='?', default='spam'),
624-
Sig('-z', nargs='?', type=int, const='42', default='84'),
624+
Sig('-z', nargs='?', type=int, const='42', default='84', choices=[1, 2]),
625625
]
626-
failures = ['2']
626+
failures = ['2', '-z a', '-z 42', '-z 84']
627627
successes = [
628628
('', NS(w=None, x=None, y='spam', z=84)),
629629
('-w', NS(w=None, x=None, y='spam', z=84)),
@@ -1001,8 +1001,8 @@ class TestPositionalsNargsZeroOrMore(ParserTestCase):
10011001
class TestPositionalsNargsZeroOrMoreDefault(ParserTestCase):
10021002
"""Test a Positional that specifies unlimited nargs and a default"""
10031003

1004-
argument_signatures = [Sig('foo', nargs='*', default='bar')]
1005-
failures = ['-x']
1004+
argument_signatures = [Sig('foo', nargs='*', default='bar', choices=['a', 'b'])]
1005+
failures = ['-x', 'bar', 'a c']
10061006
successes = [
10071007
('', NS(foo='bar')),
10081008
('a', NS(foo=['a'])),
@@ -1035,8 +1035,8 @@ class TestPositionalsNargsOptional(ParserTestCase):
10351035
class TestPositionalsNargsOptionalDefault(ParserTestCase):
10361036
"""Tests an Optional Positional with a default value"""
10371037

1038-
argument_signatures = [Sig('foo', nargs='?', default=42)]
1039-
failures = ['-x', 'a b']
1038+
argument_signatures = [Sig('foo', nargs='?', default=42, choices=['a', 'b'])]
1039+
failures = ['-x', 'a b', '42']
10401040
successes = [
10411041
('', NS(foo=42)),
10421042
('a', NS(foo='a')),
@@ -1049,9 +1049,9 @@ class TestPositionalsNargsOptionalConvertedDefault(ParserTestCase):
10491049
"""
10501050

10511051
argument_signatures = [
1052-
Sig('foo', nargs='?', type=int, default='42'),
1052+
Sig('foo', nargs='?', type=int, default='42', choices=[1, 2]),
10531053
]
1054-
failures = ['-x', 'a b', '1 2']
1054+
failures = ['-x', 'a b', '1 2', '42']
10551055
successes = [
10561056
('', NS(foo=42)),
10571057
('1', NS(foo=1)),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix support of arguments with :ref:`choices` in :mod:`argparse`. Positional
2+
arguments with :ref:`nargs` equal to ``'?'`` or ``'*'`` no longer check
3+
:ref:`default` against ``choices``. Optional arguments with ``nargs`` equal
4+
to ``'?'`` no longer check :ref:`const` against ``choices``.

0 commit comments

Comments
 (0)