[Python-checkins] [3.9] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28932)
ambv
webhook-mailer at python.org
Wed Oct 13 16:36:21 EDT 2021
https://github.com/python/cpython/commit/fb7203453855a43907048ff2b8f9b890305b3d15
commit: fb7203453855a43907048ff2b8f9b890305b3d15
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-10-13T22:36:05+02:00
summary:
[3.9] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28932)
(cherry picked from commit 6fafc25aea8689048314b5bf7a9bb986bb1ce238)
Co-authored-by: andrei kulakov <andrei.avk at gmail.com>
Co-authored-by: Łukasz Langa <lukasz at langa.pl>
files:
A Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index ecc92778c515b..cb5345d555afe 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -526,12 +526,13 @@ def _format_action(self, action):
parts = [action_header]
# if there was help for the action, add lines of help text
- if action.help:
+ if action.help and action.help.strip():
help_text = self._expand_help(action)
- help_lines = self._split_lines(help_text, help_width)
- parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
- for line in help_lines[1:]:
- parts.append('%*s%s\n' % (help_position, '', line))
+ if help_text:
+ help_lines = self._split_lines(help_text, help_width)
+ parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
+ for line in help_lines[1:]:
+ parts.append('%*s%s\n' % (help_position, '', line))
# or add a newline if the description doesn't end with one
elif not action_header.endswith('\n'):
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 82c1f6c60f828..c3c3a75fafff7 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2143,6 +2143,44 @@ def test_help_non_breaking_spaces(self):
wrap\N{NO-BREAK SPACE}at non-breaking spaces
'''))
+ def test_help_blank(self):
+ # Issue 24444
+ parser = ErrorRaisingArgumentParser(
+ prog='PROG', description='main description')
+ parser.add_argument(
+ 'foo',
+ help=' ')
+ self.assertEqual(parser.format_help(), textwrap.dedent('''\
+ usage: PROG [-h] foo
+
+ main description
+
+ positional arguments:
+ foo \
+
+
+ optional arguments:
+ -h, --help show this help message and exit
+ '''))
+
+ parser = ErrorRaisingArgumentParser(
+ prog='PROG', description='main description')
+ parser.add_argument(
+ 'foo', choices=[],
+ help='%(choices)s')
+ self.assertEqual(parser.format_help(), textwrap.dedent('''\
+ usage: PROG [-h] {}
+
+ main description
+
+ positional arguments:
+ {} \
+
+
+ optional arguments:
+ -h, --help show this help message and exit
+ '''))
+
def test_help_alternate_prefix_chars(self):
parser = self._get_parser(prefix_chars='+:/')
self.assertEqual(parser.format_usage(),
diff --git a/Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst b/Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst
new file mode 100644
index 0000000000000..efcacb8f0eb59
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst
@@ -0,0 +1,2 @@
+Fixed an error raised in :mod:`argparse` help display when help for an
+option is set to 1+ blank spaces or when *choices* arg is an empty container.
More information about the Python-checkins
mailing list