Skip to content

Commit 403abbc

Browse files
mhilsambv
authored andcommitted
[3.9] bpo-38956: don't print BooleanOptionalAction's default twice (pythonGH-27672)
Co-authored-by: Micky Yun Chan <[email protected]>. (cherry picked from commit 1512bc2) Co-authored-by: Maximilian Hils <[email protected]>
1 parent c7c4cbc commit 403abbc

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
@@ -875,7 +875,7 @@ def __init__(self,
875875
_option_strings.append(option_string)
876876

877877
if help is not None and default is not None:
878-
help += f" (default: {default})"
878+
help += " (default: %(default)s)"
879879

880880
super().__init__(
881881
option_strings=_option_strings,

Lib/test/test_argparse.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4259,6 +4259,9 @@ class TestHelpArgumentDefaults(HelpTestCase):
42594259
argument_signatures = [
42604260
Sig('--foo', help='foo help - oh and by the way, %(default)s'),
42614261
Sig('--bar', action='store_true', help='bar help'),
4262+
Sig('--taz', action=argparse.BooleanOptionalAction,
4263+
help='Whether to taz it', default=True),
4264+
Sig('--quux', help="Set the quux", default=42),
42624265
Sig('spam', help='spam help'),
42634266
Sig('badger', nargs='?', default='wooden', help='badger help'),
42644267
]
@@ -4267,25 +4270,29 @@ class TestHelpArgumentDefaults(HelpTestCase):
42674270
[Sig('--baz', type=int, default=42, help='baz help')]),
42684271
]
42694272
usage = '''\
4270-
usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
4273+
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
4274+
[--baz BAZ]
4275+
spam [badger]
42714276
'''
42724277
help = usage + '''\
42734278
42744279
description
42754280
42764281
positional arguments:
4277-
spam spam help
4278-
badger badger help (default: wooden)
4282+
spam spam help
4283+
badger badger help (default: wooden)
42794284
42804285
optional arguments:
4281-
-h, --help show this help message and exit
4282-
--foo FOO foo help - oh and by the way, None
4283-
--bar bar help (default: False)
4286+
-h, --help show this help message and exit
4287+
--foo FOO foo help - oh and by the way, None
4288+
--bar bar help (default: False)
4289+
--taz, --no-taz Whether to taz it (default: True)
4290+
--quux QUUX Set the quux (default: 42)
42844291
42854292
title:
42864293
description
42874294
4288-
--baz BAZ baz help (default: 42)
4295+
--baz BAZ baz help (default: 42)
42894296
'''
42904297
version = ''
42914298

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`argparse.BooleanOptionalAction`'s default value is no longer printed twice when used with :class:`argparse.ArgumentDefaultsHelpFormatter`.

0 commit comments

Comments
 (0)