Skip to content

Commit dafac8a

Browse files
[3.14] gh-112527: Fix help text for required options in argparse (GH-112528) (GH-142475)
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. (cherry picked from commit 1adb17b) Co-authored-by: Fabian Henze <[email protected]>
1 parent 69ecb4c commit dafac8a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

Lib/argparse.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,14 @@ def _get_help_string(self, action):
739739
if help is None:
740740
help = ''
741741

742-
if '%(default)' not in help:
743-
if action.default is not SUPPRESS:
744-
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
745-
if action.option_strings or action.nargs in defaulting_nargs:
746-
help += _(' (default: %(default)s)')
742+
if (
743+
'%(default)' not in help
744+
and action.default is not SUPPRESS
745+
and not action.required
746+
):
747+
defaulting_nargs = (OPTIONAL, ZERO_OR_MORE)
748+
if action.option_strings or action.nargs in defaulting_nargs:
749+
help += _(' (default: %(default)s)')
747750
return help
748751

749752

Lib/test/test_argparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,6 +5328,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
53285328
argument_signatures = [
53295329
Sig('--foo', help='foo help - oh and by the way, %(default)s'),
53305330
Sig('--bar', action='store_true', help='bar help'),
5331+
Sig('--required', required=True, help='some help'),
53315332
Sig('--taz', action=argparse.BooleanOptionalAction,
53325333
help='Whether to taz it', default=True),
53335334
Sig('--corge', action=argparse.BooleanOptionalAction,
@@ -5341,8 +5342,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
53415342
[Sig('--baz', type=int, default=42, help='baz help')]),
53425343
]
53435344
usage = '''\
5344-
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
5345-
[--quux QUUX] [--baz BAZ]
5345+
usage: PROG [-h] [--foo FOO] [--bar] --required REQUIRED [--taz | --no-taz]
5346+
[--corge | --no-corge] [--quux QUUX] [--baz BAZ]
53465347
spam [badger]
53475348
'''
53485349
help = usage + '''\
@@ -5357,6 +5358,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
53575358
-h, --help show this help message and exit
53585359
--foo FOO foo help - oh and by the way, None
53595360
--bar bar help (default: False)
5361+
--required REQUIRED some help
53605362
--taz, --no-taz Whether to taz it (default: True)
53615363
--corge, --no-corge Whether to corge it
53625364
--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)