mirror of
https://github.com/python/cpython.git
synced 2025-09-09 02:11:51 +00:00
bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099)
This commit is contained in:
parent
f54fee7f37
commit
86de99588d
3 changed files with 11 additions and 0 deletions
|
@ -392,6 +392,9 @@ class HelpFormatter(object):
|
||||||
group_actions = set()
|
group_actions = set()
|
||||||
inserts = {}
|
inserts = {}
|
||||||
for group in groups:
|
for group in groups:
|
||||||
|
if not group._group_actions:
|
||||||
|
raise ValueError(f'empty group {group}')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start = actions.index(group._group_actions[0])
|
start = actions.index(group._group_actions[0])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -2601,6 +2601,13 @@ class TestMutuallyExclusiveGroupErrors(TestCase):
|
||||||
'''
|
'''
|
||||||
self.assertEqual(parser.format_help(), textwrap.dedent(expected))
|
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):
|
class MEMixin(object):
|
||||||
|
|
||||||
def test_failures_when_not_required(self):
|
def test_failures_when_not_required(self):
|
||||||
|
|
|
@ -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`.
|
Loading…
Add table
Add a link
Reference in a new issue