bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099) (GH-30114)

(cherry picked from commit 86de99588d)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2021-12-15 04:20:04 -08:00 committed by GitHub
parent 908fd691f9
commit 8e4c96295b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 0 deletions

View file

@ -392,6 +392,9 @@ class HelpFormatter(object):
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:

View file

@ -2582,6 +2582,13 @@ class TestMutuallyExclusiveGroupErrors(TestCase):
'''
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):

View file

@ -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`.