[Python-checkins] bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099) (GH-30114)
iritkatriel
webhook-mailer at python.org
Wed Dec 15 07:20:26 EST 2021
https://github.com/python/cpython/commit/8e4c96295bd78ae5f70b908e5dbac0da7c4c21bd
commit: 8e4c96295bd78ae5f70b908e5dbac0da7c4c21bd
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-12-15T12:20:04Z
summary:
bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099) (GH-30114)
(cherry picked from commit 86de99588db3beff964137f4fe27dd1077a09b35)
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2021-12-14-13-18-45.bpo-26952.hjhISq.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 9eed24c2a2a76..b2db312b8fdfd 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -392,6 +392,9 @@ def _format_actions_usage(self, actions, groups):
group_actions = set()
inserts = {}
for group in groups:
+ if not group._group_actions:
+ raise ValueError(f'empty group {group}')
+
try:
start = actions.index(group._group_actions[0])
except ValueError:
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index ec49b2acbbb85..c96a540a8b315 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2582,6 +2582,13 @@ def test_help(self):
'''
self.assertEqual(parser.format_help(), textwrap.dedent(expected))
+ def test_empty_group(self):
+ # See issue 26952
+ parser = argparse.ArgumentParser()
+ group = parser.add_mutually_exclusive_group()
+ with self.assertRaises(ValueError):
+ parser.parse_args(['-h'])
+
class MEMixin(object):
def test_failures_when_not_required(self):
diff --git a/Misc/NEWS.d/next/Library/2021-12-14-13-18-45.bpo-26952.hjhISq.rst b/Misc/NEWS.d/next/Library/2021-12-14-13-18-45.bpo-26952.hjhISq.rst
new file mode 100644
index 0000000000000..379dbb55c7ca8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-14-13-18-45.bpo-26952.hjhISq.rst
@@ -0,0 +1 @@
+:mod:`argparse` raises :exc:`ValueError` with clear message when trying to render usage for an empty mutually-exclusive group. Previously it raised a cryptic :exc:`IndexError`.
\ No newline at end of file
More information about the Python-checkins
mailing list