diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2021-12-15 10:08:26 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 10:08:26 +0000 |
commit | 86de99588db3beff964137f4fe27dd1077a09b35 (patch) | |
tree | 08f7eb50ed9a1d7bdc23d0be4795632716548ae4 /Lib | |
parent | f54fee7f37563fbd569596cf94aad023ac6c3179 (diff) | |
download | cpython-git-86de99588db3beff964137f4fe27dd1077a09b35.tar.gz |
bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/argparse.py | 3 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 7 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index b44fa4f0f6..8a81801ba9 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index f3edde3de8..eb37d4d365 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2601,6 +2601,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): |