diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2021-12-16 15:31:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-16 15:31:08 (GMT) |
commit | 30322c497e0b8d978f7a0de95985aac9c5daf1ac (patch) | |
tree | 4fb3f46fa7b17cdfc21157b426e5b2e2ec1443bb /Lib | |
parent | d6e13747161d7b634b47d2d3d212ed3be4a21fab (diff) | |
download | cpython-30322c497e0b8d978f7a0de95985aac9c5daf1ac.zip cpython-30322c497e0b8d978f7a0de95985aac9c5daf1ac.tar.gz cpython-30322c497e0b8d978f7a0de95985aac9c5daf1ac.tar.bz2 |
bpo-22047: [argparse] deprecate nested argument groups and mutually exclusive groups (GH-30098)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/argparse.py | 18 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 14 |
2 files changed, 30 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 8a81801..de95eed 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -89,6 +89,8 @@ import os as _os import re as _re import sys as _sys +import warnings + from gettext import gettext as _, ngettext SUPPRESS = '==SUPPRESS==' @@ -1648,6 +1650,14 @@ class _ArgumentGroup(_ActionsContainer): super(_ArgumentGroup, self)._remove_action(action) self._group_actions.remove(action) + def add_argument_group(self, *args, **kwargs): + warnings.warn( + "Nesting argument groups is deprecated.", + category=DeprecationWarning, + stacklevel=2 + ) + return super().add_argument_group(*args, **kwargs) + class _MutuallyExclusiveGroup(_ArgumentGroup): @@ -1668,6 +1678,14 @@ class _MutuallyExclusiveGroup(_ArgumentGroup): self._container._remove_action(action) self._group_actions.remove(action) + def add_mutually_exclusive_group(self, *args, **kwargs): + warnings.warn( + "Nesting mutually exclusive groups is deprecated.", + category=DeprecationWarning, + stacklevel=2 + ) + return super().add_mutually_exclusive_group(*args, **kwargs) + class ArgumentParser(_AttributeHolder, _ActionsContainer): """Object for parsing command line strings into Python objects. diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index eb37d4d..4c23610 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -9,6 +9,7 @@ import textwrap import tempfile import unittest import argparse +import warnings from io import StringIO @@ -2973,15 +2974,24 @@ class TestMutuallyExclusiveOptionalsAndPositionalsMixed(MEMixin, TestCase): class TestMutuallyExclusiveNested(MEMixin, TestCase): + # Nesting mutually exclusive groups is an undocumented feature + # that came about by accident through inheritance and has been + # the source of many bugs. It is deprecated and this test should + # eventually be removed along with it. + def get_parser(self, required): parser = ErrorRaisingArgumentParser(prog='PROG') group = parser.add_mutually_exclusive_group(required=required) group.add_argument('-a') group.add_argument('-b') - group2 = group.add_mutually_exclusive_group(required=required) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + group2 = group.add_mutually_exclusive_group(required=required) group2.add_argument('-c') group2.add_argument('-d') - group3 = group2.add_mutually_exclusive_group(required=required) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + group3 = group2.add_mutually_exclusive_group(required=required) group3.add_argument('-e') group3.add_argument('-f') return parser |