Skip to content

Commit f28906e

Browse files
[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 9bcadf5) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent aa648c2 commit f28906e

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,7 @@ def _get_values(self, action, arg_strings):
25082508
value = action.const
25092509
else:
25102510
value = action.default
2511-
if isinstance(value, str):
2511+
if isinstance(value, str) and value is not SUPPRESS:
25122512
value = self._get_value(action, value)
25132513
self._check_value(action, value)
25142514

Lib/test/test_argparse.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,18 +1630,24 @@ class TestDefaultSuppress(ParserTestCase):
16301630
"""Test actions with suppressed defaults"""
16311631

16321632
argument_signatures = [
1633-
Sig('foo', nargs='?', default=argparse.SUPPRESS),
1634-
Sig('bar', nargs='*', default=argparse.SUPPRESS),
1633+
Sig('foo', nargs='?', type=int, default=argparse.SUPPRESS),
1634+
Sig('bar', nargs='*', type=int, default=argparse.SUPPRESS),
16351635
Sig('--baz', action='store_true', default=argparse.SUPPRESS),
1636+
Sig('--qux', nargs='?', type=int, default=argparse.SUPPRESS),
1637+
Sig('--quux', nargs='*', type=int, default=argparse.SUPPRESS),
16361638
]
1637-
failures = ['-x']
1639+
failures = ['-x', 'a', '1 a']
16381640
successes = [
16391641
('', NS()),
1640-
('a', NS(foo='a')),
1641-
('a b', NS(foo='a', bar=['b'])),
1642+
('1', NS(foo=1)),
1643+
('1 2', NS(foo=1, bar=[2])),
16421644
('--baz', NS(baz=True)),
1643-
('a --baz', NS(foo='a', baz=True)),
1644-
('--baz a b', NS(foo='a', bar=['b'], baz=True)),
1645+
('1 --baz', NS(foo=1, baz=True)),
1646+
('--baz 1 2', NS(foo=1, bar=[2], baz=True)),
1647+
('--qux', NS(qux=None)),
1648+
('--qux 1', NS(qux=1)),
1649+
('--quux', NS(quux=[])),
1650+
('--quux 1 2', NS(quux=[1, 2])),
16451651
]
16461652

16471653

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`argparse` support of positional arguments with ``nargs='?'``,
2+
``default=argparse.SUPPRESS`` and specified ``type``.

0 commit comments

Comments
 (0)