https://github.com/python/cpython/commit/1adb17b1a26e1547d14ca15f915e605cfdda3edd
commit: 1adb17b1a26e1547d14ca15f915e605cfdda3edd
branch: main
author: Fabian Henze <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-12-09T16:48:35Z
summary:
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.
files:
A Misc/NEWS.d/next/Library/2025-12-09-14-40-45.gh-issue-112527.Tvf5Zk.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 1d550264ae420f..ed98aa9e974b2a 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -738,18 +738,21 @@ def _get_help_string(self, action):
if help is None:
help = ''
- if '%(default)' not in help:
- if action.default is not SUPPRESS:
- defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
- if action.option_strings or action.nargs in defaulting_nargs:
- t = self._theme
- default_str = _(" (default: %(default)s)")
- prefix, suffix = default_str.split("%(default)s")
- help += (
- f" {t.default}{prefix.lstrip()}"
- f"{t.default_value}%(default)s"
- f"{t.default}{suffix}{t.reset}"
- )
+ if (
+ '%(default)' not in help
+ and action.default is not SUPPRESS
+ and not action.required
+ ):
+ defaulting_nargs = (OPTIONAL, ZERO_OR_MORE)
+ if action.option_strings or action.nargs in defaulting_nargs:
+ t = self._theme
+ default_str = _(" (default: %(default)s)")
+ prefix, suffix = default_str.split("%(default)s")
+ help += (
+ f" {t.default}{prefix.lstrip()}"
+ f"{t.default_value}%(default)s"
+ f"{t.default}{suffix}{t.reset}"
+ )
return help
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index ab5382e41e7871..24e8ab1c5cacbb 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -5419,6 +5419,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
argument_signatures = [
Sig('--foo', help='foo help - oh and by the way, %(default)s'),
Sig('--bar', action='store_true', help='bar help'),
+ Sig('--required', required=True, help='some help'),
Sig('--taz', action=argparse.BooleanOptionalAction,
help='Whether to taz it', default=True),
Sig('--corge', action=argparse.BooleanOptionalAction,
@@ -5432,8 +5433,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
[Sig('--baz', type=int, default=42, help='baz help')]),
]
usage = '''\
- usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge |
--no-corge]
- [--quux QUUX] [--baz BAZ]
+ usage: PROG [-h] [--foo FOO] [--bar] --required REQUIRED [--taz |
--no-taz]
+ [--corge | --no-corge] [--quux QUUX] [--baz BAZ]
spam [badger]
'''
help = usage + '''\
@@ -5448,6 +5449,7 @@ class TestHelpArgumentDefaults(HelpTestCase):
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
+ --required REQUIRED some help
--taz, --no-taz Whether to taz it (default: True)
--corge, --no-corge Whether to corge it
--quux QUUX Set the quux (default: 42)
diff --git
a/Misc/NEWS.d/next/Library/2025-12-09-14-40-45.gh-issue-112527.Tvf5Zk.rst
b/Misc/NEWS.d/next/Library/2025-12-09-14-40-45.gh-issue-112527.Tvf5Zk.rst
new file mode 100644
index 00000000000000..70447bc6437677
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-09-14-40-45.gh-issue-112527.Tvf5Zk.rst
@@ -0,0 +1,2 @@
+The help text for required options in :mod:`argparse` no
+longer extended with " (default: None)".
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]