Skip to content

Commit 1adb17b

Browse files
authored
gh-112527: Fix help text for required options in argparse (GH-112528)
For optional arguments with required=True, the ArgumentDefaultsHelpFormatter would always add a " (default: None)" to the end of the help text. Since that's a bit misleading, it is removed with this commit.
1 parent b20722c commit 1adb17b

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

Lib/argparse.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -738,18 +738,21 @@ def _get_help_string(self, action):
738738
if help is None:
739739
help = ''
740740

741-
if '%(default)' not in help:
742-
if action.default is not SUPPRESS:
743-
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
744-
if action.option_strings or action.nargs in defaulting_nargs:
745-
t = self._theme
746-
default_str = _(" (default: %(default)s)")
747-
prefix, suffix = default_str.split("%(default)s")
748-
help += (
749-
f" {t.default}{prefix.lstrip()}"
750-
f"{t.default_value}%(default)s"
751-
f"{t.default}{suffix}{t.reset}"
752-
)
741+
if (
742+
'%(default)' not in help
743+
and action.default is not SUPPRESS
744+
and not action.required
745+
):
746+
defaulting_nargs = (OPTIONAL, ZERO_OR_MORE)
747+
if action.option_strings or action.nargs in defaulting_nargs:
748+
t = self._theme
749+
default_str = _(" (default: %(default)s)")
750+
prefix, suffix = default_str.split("%(default)s")
751+
help += (
752+
f" {t.default}{prefix.lstrip()}"
753+
f"{t.default_value}%(default)s"
754+
f"{t.default}{suffix}{t.reset}"
755+
)
753756
return help
754757

755758

Lib/test/test_argparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,6 +5419,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
54195419
argument_signatures = [
54205420
Sig('--foo', help='foo help - oh and by the way, %(default)s'),
54215421
Sig('--bar', action='store_true', help='bar help'),
5422+
Sig('--required', required=True, help='some help'),
54225423
Sig('--taz', action=argparse.BooleanOptionalAction,
54235424
help='Whether to taz it', default=True),
54245425
Sig('--corge', action=argparse.BooleanOptionalAction,
@@ -5432,8 +5433,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
54325433
[Sig('--baz', type=int, default=42, help='baz help')]),
54335434
]
54345435
usage = '''\
5435-
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
5436-
[--quux QUUX] [--baz BAZ]
5436+
usage: PROG [-h] [--foo FOO] [--bar] --required REQUIRED [--taz | --no-taz]
5437+
[--corge | --no-corge] [--quux QUUX] [--baz BAZ]
54375438
spam [badger]
54385439
'''
54395440
help = usage + '''\
@@ -5448,6 +5449,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
54485449
-h, --help show this help message and exit
54495450
--foo FOO foo help - oh and by the way, None
54505451
--bar bar help (default: False)
5452+
--required REQUIRED some help
54515453
--taz, --no-taz Whether to taz it (default: True)
54525454
--corge, --no-corge Whether to corge it
54535455
--quux QUUX Set the quux (default: 42)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The help text for required options in :mod:`argparse` no
2+
longer extended with " (default: None)".

0 commit comments

Comments
 (0)