Skip to content

Commit a5ffa74

Browse files
gh-61011: Fix inheritance of nested mutually exclusive groups in argparse
Previously, all nested mutually exclusive groups lost their connection to the group containing them and were displayed as belonging directly to the parser. Co-authored-by: Dougal Sutherland <[email protected]>
1 parent 9bda775 commit a5ffa74

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

Lib/argparse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,11 @@ def _add_container_actions(self, container):
15211521
# NOTE: if add_mutually_exclusive_group ever gains title= and
15221522
# description= then this code will need to be expanded as above
15231523
for group in container._mutually_exclusive_groups:
1524-
mutex_group = self.add_mutually_exclusive_group(
1524+
if group._container is container:
1525+
cont = self
1526+
else:
1527+
cont = title_group_map[group._container.title]
1528+
mutex_group = cont.add_mutually_exclusive_group(
15251529
required=group.required)
15261530

15271531
# map the actions to their new mutex group

Lib/test/test_argparse.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,35 @@ def test_groups_parents(self):
29292929
def test_wrong_type_parents(self):
29302930
self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1])
29312931

2932+
def test_mutex_groups_parents(self):
2933+
parent = ErrorRaisingArgumentParser(add_help=False)
2934+
g = parent.add_argument_group(title='g', description='gd')
2935+
g.add_argument('-w')
2936+
g.add_argument('-x')
2937+
m = g.add_mutually_exclusive_group()
2938+
m.add_argument('-y')
2939+
m.add_argument('-z')
2940+
parser = ErrorRaisingArgumentParser(prog='PROG', parents=[parent])
2941+
2942+
self.assertRaises(ArgumentParserError, parser.parse_args,
2943+
['-y', 'Y', '-z', 'Z'])
2944+
2945+
parser_help = parser.format_help()
2946+
self.assertEqual(parser_help, textwrap.dedent('''\
2947+
usage: PROG [-h] [-w W] [-x X] [-y Y | -z Z]
2948+
2949+
options:
2950+
-h, --help show this help message and exit
2951+
2952+
g:
2953+
gd
2954+
2955+
-w W
2956+
-x X
2957+
-y Y
2958+
-z Z
2959+
'''))
2960+
29322961
# ==============================
29332962
# Mutually exclusive group tests
29342963
# ==============================

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ Reuben Sumner
18141814
Eryk Sun
18151815
Sanjay Sundaresan
18161816
Marek Šuppa
1817+
Dougal J. Sutherland
18171818
Hisao Suzuki
18181819
Kalle Svensson
18191820
Andrew Svetlov
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix inheritance of nested mutually exclusive groups from parent parser in
2+
:class:`argparse.ArgumentParser`. Previously, all nested mutually exclusive
3+
groups lost their connection to the group containing them and were displayed
4+
as belonging directly to the parser.

0 commit comments

Comments
 (0)