mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.13] gh-80259: Fix conflict between type and default=SUPPRESS in argparse (GH-124519) (GH-124751)
type() no longer called for SUPPRESS.
This only affects positional arguments with nargs='?'.
(cherry picked from commit 9bcadf589a
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
aa648c21e9
commit
f28906e58e
3 changed files with 16 additions and 8 deletions
|
@ -2508,7 +2508,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
value = action.const
|
value = action.const
|
||||||
else:
|
else:
|
||||||
value = action.default
|
value = action.default
|
||||||
if isinstance(value, str):
|
if isinstance(value, str) and value is not SUPPRESS:
|
||||||
value = self._get_value(action, value)
|
value = self._get_value(action, value)
|
||||||
self._check_value(action, value)
|
self._check_value(action, value)
|
||||||
|
|
||||||
|
|
|
@ -1630,18 +1630,24 @@ class TestDefaultSuppress(ParserTestCase):
|
||||||
"""Test actions with suppressed defaults"""
|
"""Test actions with suppressed defaults"""
|
||||||
|
|
||||||
argument_signatures = [
|
argument_signatures = [
|
||||||
Sig('foo', nargs='?', default=argparse.SUPPRESS),
|
Sig('foo', nargs='?', type=int, default=argparse.SUPPRESS),
|
||||||
Sig('bar', nargs='*', default=argparse.SUPPRESS),
|
Sig('bar', nargs='*', type=int, default=argparse.SUPPRESS),
|
||||||
Sig('--baz', action='store_true', default=argparse.SUPPRESS),
|
Sig('--baz', action='store_true', default=argparse.SUPPRESS),
|
||||||
|
Sig('--qux', nargs='?', type=int, default=argparse.SUPPRESS),
|
||||||
|
Sig('--quux', nargs='*', type=int, default=argparse.SUPPRESS),
|
||||||
]
|
]
|
||||||
failures = ['-x']
|
failures = ['-x', 'a', '1 a']
|
||||||
successes = [
|
successes = [
|
||||||
('', NS()),
|
('', NS()),
|
||||||
('a', NS(foo='a')),
|
('1', NS(foo=1)),
|
||||||
('a b', NS(foo='a', bar=['b'])),
|
('1 2', NS(foo=1, bar=[2])),
|
||||||
('--baz', NS(baz=True)),
|
('--baz', NS(baz=True)),
|
||||||
('a --baz', NS(foo='a', baz=True)),
|
('1 --baz', NS(foo=1, baz=True)),
|
||||||
('--baz a b', NS(foo='a', bar=['b'], baz=True)),
|
('--baz 1 2', NS(foo=1, bar=[2], baz=True)),
|
||||||
|
('--qux', NS(qux=None)),
|
||||||
|
('--qux 1', NS(qux=1)),
|
||||||
|
('--quux', NS(quux=[])),
|
||||||
|
('--quux 1 2', NS(quux=[1, 2])),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix :mod:`argparse` support of positional arguments with ``nargs='?'``,
|
||||||
|
``default=argparse.SUPPRESS`` and specified ``type``.
|
Loading…
Add table
Add a link
Reference in a new issue