Skip to content

Commit 18c7449

Browse files
gh-61011: Fix inheritance of nested mutually exclusive groups in argparse (GH-125210)
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: Danica J. Sutherland <[email protected]>
1 parent 0135848 commit 18c7449

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
@@ -2942,6 +2942,35 @@ def test_groups_parents(self):
29422942
def test_wrong_type_parents(self):
29432943
self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1])
29442944

2945+
def test_mutex_groups_parents(self):
2946+
parent = ErrorRaisingArgumentParser(add_help=False)
2947+
g = parent.add_argument_group(title='g', description='gd')
2948+
g.add_argument('-w')
2949+
g.add_argument('-x')
2950+
m = g.add_mutually_exclusive_group()
2951+
m.add_argument('-y')
2952+
m.add_argument('-z')
2953+
parser = ErrorRaisingArgumentParser(prog='PROG', parents=[parent])
2954+
2955+
self.assertRaises(ArgumentParserError, parser.parse_args,
2956+
['-y', 'Y', '-z', 'Z'])
2957+
2958+
parser_help = parser.format_help()
2959+
self.assertEqual(parser_help, textwrap.dedent('''\
2960+
usage: PROG [-h] [-w W] [-x X] [-y Y | -z Z]
2961+
2962+
options:
2963+
-h, --help show this help message and exit
2964+
2965+
g:
2966+
gd
2967+
2968+
-w W
2969+
-x X
2970+
-y Y
2971+
-z Z
2972+
'''))
2973+
29452974
# ==============================
29462975
# Mutually exclusive group tests
29472976
# ==============================

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+
Danica 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)