Skip to content

Commit 23cefd9

Browse files
miss-islingtonserhiy-storchakadjsutherland
authored
[3.12] gh-61011: Fix inheritance of nested mutually exclusive groups in argparse (GH-125210) (GH-125309)
Previously, all nested mutually exclusive groups lost their connection to the group containing them and were displayed as belonging directly to the parser. (cherry picked from commit 18c7449) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Danica J. Sutherland <[email protected]>
1 parent 4c40381 commit 23cefd9

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
@@ -1564,7 +1564,11 @@ def _add_container_actions(self, container):
15641564
# NOTE: if add_mutually_exclusive_group ever gains title= and
15651565
# description= then this code will need to be expanded as above
15661566
for group in container._mutually_exclusive_groups:
1567-
mutex_group = self.add_mutually_exclusive_group(
1567+
if group._container is container:
1568+
cont = self
1569+
else:
1570+
cont = title_group_map[group._container.title]
1571+
mutex_group = cont.add_mutually_exclusive_group(
15681572
required=group.required)
15691573

15701574
# 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
@@ -2880,6 +2880,35 @@ def test_groups_parents(self):
28802880
-x X
28812881
'''.format(progname, ' ' if progname else '' )))
28822882

2883+
def test_mutex_groups_parents(self):
2884+
parent = ErrorRaisingArgumentParser(add_help=False)
2885+
g = parent.add_argument_group(title='g', description='gd')
2886+
g.add_argument('-w')
2887+
g.add_argument('-x')
2888+
m = g.add_mutually_exclusive_group()
2889+
m.add_argument('-y')
2890+
m.add_argument('-z')
2891+
parser = ErrorRaisingArgumentParser(prog='PROG', parents=[parent])
2892+
2893+
self.assertRaises(ArgumentParserError, parser.parse_args,
2894+
['-y', 'Y', '-z', 'Z'])
2895+
2896+
parser_help = parser.format_help()
2897+
self.assertEqual(parser_help, textwrap.dedent('''\
2898+
usage: PROG [-h] [-w W] [-x X] [-y Y | -z Z]
2899+
2900+
options:
2901+
-h, --help show this help message and exit
2902+
2903+
g:
2904+
gd
2905+
2906+
-w W
2907+
-x X
2908+
-y Y
2909+
-z Z
2910+
'''))
2911+
28832912
# ==============================
28842913
# Mutually exclusive group tests
28852914
# ==============================

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ Reuben Sumner
17921792
Eryk Sun
17931793
Sanjay Sundaresan
17941794
Marek Šuppa
1795+
Danica J. Sutherland
17951796
Hisao Suzuki
17961797
Kalle Svensson
17971798
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)