gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions (#27808)

Help for other actions omit the default value if default is SUPPRESS or
already contains the special format string '%(default)'.  Add those
special cases to BooleanOptionalAction's help formatting too.

Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not
emitted.

Fixes https://bugs.python.org/issue38956 as this code will detect
whether '%(default)s' has already been specified in the help string.

Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com>
Co-authored-by: Micky Yun Chan <michan@redhat.com>
This commit is contained in:
Toshio Kuratomi 2022-05-03 09:38:18 -07:00 committed by GitHub
parent 6c25bf07e8
commit 20490d5018
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 16 deletions

View file

@ -3348,6 +3348,7 @@ class TestHelpFormattingMetaclass(type):
def _test(self, tester, parser_text):
expected_text = getattr(tester, self.func_suffix)
expected_text = textwrap.dedent(expected_text)
tester.maxDiff = None
tester.assertEqual(expected_text, parser_text)
def test_format(self, tester):
@ -3743,7 +3744,7 @@ class TestHelpUsage(HelpTestCase):
-w W [W ...] w
-x [X ...] x
--foo, --no-foo Whether to foo
--bar, --no-bar Whether to bar (default: True)
--bar, --no-bar Whether to bar
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
--bazz, --no-bazz Bazz!
@ -4423,6 +4424,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
Sig('--bar', action='store_true', help='bar help'),
Sig('--taz', action=argparse.BooleanOptionalAction,
help='Whether to taz it', default=True),
Sig('--corge', action=argparse.BooleanOptionalAction,
help='Whether to corge it', default=argparse.SUPPRESS),
Sig('--quux', help="Set the quux", default=42),
Sig('spam', help='spam help'),
Sig('badger', nargs='?', default='wooden', help='badger help'),
@ -4432,8 +4435,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
[Sig('--baz', type=int, default=42, help='baz help')]),
]
usage = '''\
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
[--baz BAZ]
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
[--quux QUUX] [--baz BAZ]
spam [badger]
'''
help = usage + '''\
@ -4441,20 +4444,21 @@ class TestHelpArgumentDefaults(HelpTestCase):
description
positional arguments:
spam spam help
badger badger help (default: wooden)
spam spam help
badger badger help (default: wooden)
options:
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
--taz, --no-taz Whether to taz it (default: True)
--quux QUUX Set the quux (default: 42)
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
--taz, --no-taz Whether to taz it (default: True)
--corge, --no-corge Whether to corge it
--quux QUUX Set the quux (default: 42)
title:
description
--baz BAZ baz help (default: 42)
--baz BAZ baz help (default: 42)
'''
version = ''