[Python-checkins] bpo-22047: [argparse] deprecate nested argument groups and mutually exclusive groups (GH-30098)

iritkatriel webhook-mailer at python.org
Thu Dec 16 10:31:22 EST 2021


https://github.com/python/cpython/commit/30322c497e0b8d978f7a0de95985aac9c5daf1ac
commit: 30322c497e0b8d978f7a0de95985aac9c5daf1ac
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-12-16T15:31:08Z
summary:

bpo-22047: [argparse] deprecate nested argument groups and mutually exclusive groups (GH-30098)

files:
A Misc/NEWS.d/next/Library/2021-12-15-19-24-54.bpo-22047.gBV4vT.rst
M Doc/library/argparse.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 2edf84e5f0444..80c382a981b8d 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -1898,6 +1898,12 @@ Argument groups
    Note that any arguments not in your user-defined groups will end up back
    in the usual "positional arguments" and "optional arguments" sections.
 
+   .. versionchanged:: 3.11
+    Calling :meth:`add_argument_group` on an argument group is deprecated.
+    This feature was never supported and does not always work correctly.
+    The function exists on the API by accident through inheritance and
+    will be removed in the future.
+
 
 Mutual exclusion
 ^^^^^^^^^^^^^^^^
@@ -1936,6 +1942,12 @@ Mutual exclusion
    *title* and *description* arguments of
    :meth:`~ArgumentParser.add_argument_group`.
 
+   .. versionchanged:: 3.11
+    Calling :meth:`add_argument_group` or :meth:`add_mutually_exclusive_group`
+    on a mutually exclusive group is deprecated. These features were never
+    supported and do not always work correctly. The functions exist on the
+    API by accident through inheritance and will be removed in the future.
+
 
 Parser defaults
 ^^^^^^^^^^^^^^^
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 8a81801ba9236..de95eedbee0ee 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -89,6 +89,8 @@
 import re as _re
 import sys as _sys
 
+import warnings
+
 from gettext import gettext as _, ngettext
 
 SUPPRESS = '==SUPPRESS=='
@@ -1648,6 +1650,14 @@ def _remove_action(self, action):
         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 @@ def _remove_action(self, action):
         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 eb37d4d365d24..4c23610b9e961 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -9,6 +9,7 @@
 import tempfile
 import unittest
 import argparse
+import warnings
 
 from io import StringIO
 
@@ -2973,15 +2974,24 @@ def get_parser(self, required):
 
 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
diff --git a/Misc/NEWS.d/next/Library/2021-12-15-19-24-54.bpo-22047.gBV4vT.rst b/Misc/NEWS.d/next/Library/2021-12-15-19-24-54.bpo-22047.gBV4vT.rst
new file mode 100644
index 0000000000000..a381ad88af6c5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-15-19-24-54.bpo-22047.gBV4vT.rst
@@ -0,0 +1,3 @@
+Calling :meth:`add_argument_group` on an argument group is deprecated. Calling :meth:`add_argument_group` or :meth:`add_mutually_exclusive_group` on a mutually exclusive group is deprecated.
+
+These features were never supported and do not always work correctly. The functions exist on the API by accident through inheritance and will be removed in the future.
\ No newline at end of file



More information about the Python-checkins mailing list