Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Change error type
  • Loading branch information
savannahostrowski committed Nov 23, 2024
commit 41464a52f0c357a0bd4fd41652807e2eadf15f40
6 changes: 6 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,9 @@ def _remove_action(self, action):
super(_ArgumentGroup, self)._remove_action(action)
self._group_actions.remove(action)

def add_argument_group(self, *args, **kwargs):
raise ValueError('nested argument groups are not supported')
Comment thread
savannahostrowski marked this conversation as resolved.
Outdated
Comment thread
savannahostrowski marked this conversation as resolved.
Outdated

class _MutuallyExclusiveGroup(_ArgumentGroup):

def __init__(self, container, required=False):
Expand All @@ -1727,6 +1730,9 @@ def _remove_action(self, action):
self._container._remove_action(action)
self._group_actions.remove(action)

def add_mutually_exclusive_group(self, **kwargs):
raise ValueError('nested mutually exclusive groups are not supported')
Comment thread
savannahostrowski marked this conversation as resolved.
Outdated

def _prog_name(prog=None):
if prog is not None:
return prog
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,13 @@ def test_group_prefix_chars_default(self):
self.assertEqual(msg, str(cm.warning))
self.assertEqual(cm.filename, __file__)

def test_nested_argument_group(self):
parser = argparse.ArgumentParser()
g = parser.add_argument_group()
self.assertRaisesRegex(ValueError,
'nested argument groups are not supported',
g.add_argument_group)

# ===================
# Parent parser tests
# ===================
Expand Down Expand Up @@ -3254,6 +3261,14 @@ def test_empty_group(self):
with self.assertRaises(ValueError):
parser.parse_args(['-h'])

def test_nested_mutex_groups(self):
parser = argparse.ArgumentParser(prog='PROG')
g = parser.add_mutually_exclusive_group()
g.add_argument("--spam")
self.assertRaisesRegex(ValueError,
'nested mutually exclusive groups are not supported',
g.add_mutually_exclusive_group)
Comment thread
savannahostrowski marked this conversation as resolved.
Outdated

class MEMixin(object):

def test_failures_when_not_required(self):
Expand Down