Skip to content

Commit dac4ec5

Browse files
pythongh-53834: Fix support of arguments with choices in argparse (pythonGH-124495)
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 6118044 commit dac4ec5

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
@@ -2487,19 +2487,15 @@ def _get_values(self, action, arg_strings):
24872487
value = action.default
24882488
if isinstance(value, str) and value is not SUPPRESS:
24892489
value = self._get_value(action, value)
2490-
self._check_value(action, value)
24912490

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

25042500
# single argument or optional argument produces a single value
25052501
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
@@ -628,9 +628,9 @@ class TestOptionalsNargsOptional(ParserTestCase):
628628
Sig('-w', nargs='?'),
629629
Sig('-x', nargs='?', const=42),
630630
Sig('-y', nargs='?', default='spam'),
631-
Sig('-z', nargs='?', type=int, const='42', default='84'),
631+
Sig('-z', nargs='?', type=int, const='42', default='84', choices=[1, 2]),
632632
]
633-
failures = ['2']
633+
failures = ['2', '-z a', '-z 42', '-z 84']
634634
successes = [
635635
('', NS(w=None, x=None, y='spam', z=84)),
636636
('-w', NS(w=None, x=None, y='spam', z=84)),
@@ -1027,8 +1027,8 @@ class TestPositionalsNargsZeroOrMore(ParserTestCase):
10271027
class TestPositionalsNargsZeroOrMoreDefault(ParserTestCase):
10281028
"""Test a Positional that specifies unlimited nargs and a default"""
10291029

1030-
argument_signatures = [Sig('foo', nargs='*', default='bar')]
1031-
failures = ['-x']
1030+
argument_signatures = [Sig('foo', nargs='*', default='bar', choices=['a', 'b'])]
1031+
failures = ['-x', 'bar', 'a c']
10321032
successes = [
10331033
('', NS(foo='bar')),
10341034
('a', NS(foo=['a'])),
@@ -1061,8 +1061,8 @@ class TestPositionalsNargsOptional(ParserTestCase):
10611061
class TestPositionalsNargsOptionalDefault(ParserTestCase):
10621062
"""Tests an Optional Positional with a default value"""
10631063

1064-
argument_signatures = [Sig('foo', nargs='?', default=42)]
1065-
failures = ['-x', 'a b']
1064+
argument_signatures = [Sig('foo', nargs='?', default=42, choices=['a', 'b'])]
1065+
failures = ['-x', 'a b', '42']
10661066
successes = [
10671067
('', NS(foo=42)),
10681068
('a', NS(foo='a')),
@@ -1075,9 +1075,9 @@ class TestPositionalsNargsOptionalConvertedDefault(ParserTestCase):
10751075
"""
10761076

10771077
argument_signatures = [
1078-
Sig('foo', nargs='?', type=int, default='42'),
1078+
Sig('foo', nargs='?', type=int, default='42', choices=[1, 2]),
10791079
]
1080-
failures = ['-x', 'a b', '1 2']
1080+
failures = ['-x', 'a b', '1 2', '42']
10811081
successes = [
10821082
('', NS(foo=42)),
10831083
('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)