]> granicus.if.org Git - python/commitdiff
bpo-33109: argparse subparsers are once again not required by default (GH-6919) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 24 May 2018 02:22:46 +0000 (19:22 -0700)
committerNed Deily <nad@python.org>
Thu, 24 May 2018 02:22:46 +0000 (22:22 -0400)
bpo-26510 in 3.7.0a2 changed the behavior of argparse to make
subparsers required by default, returning to the behavior of 2.7
and 3.2. The behavior was changed in 3.3 to be no longer required.
While it might make more sense to have the default to required,
compatibility with 3.3 through 3.6 is probably less disruptive
than trying to reintroduce compatibility with 2.7 at this point.
This change restores the 3.6 behavior.
(cherry picked from commit 8ebf5ceb0f5408d1ebc26c19702ac0762ef5ea04)

Co-authored-by: Ned Deily <nad@python.org>
Doc/library/argparse.rst
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/3.7.0a2.rst
Misc/NEWS.d/next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst [new file with mode: 0644]

index 3f327c0cfa77b7da41adea426287cad842407091..260e5cbf7000356d3657653ec634482dde2392f6 100644 (file)
@@ -1577,7 +1577,7 @@ Sub-commands
      stored; by default ``None`` and no value is stored
 
    * required_ - Whether or not a subcommand must be provided, by default
-     ``True``.
+     ``False``.
 
    * help_ - help for sub-parser group in help output, by default ``None``
 
index e3da7f0443cfa282663dc586d998092483e0dc45..e0e367bf20ca9699311745b68e986e29fc800a0b 100644 (file)
@@ -1077,7 +1077,7 @@ class _SubParsersAction(Action):
                  prog,
                  parser_class,
                  dest=SUPPRESS,
-                 required=True,
+                 required=False,
                  help=None,
                  metavar=None):
 
index c4440e4df7c15b680a0473b3f86ec573fec62bf7..bcf15ce123e463ad7a0ab1aceba25fde56af58bc 100644 (file)
@@ -1932,7 +1932,9 @@ class TestAddSubparsers(TestCase):
         parser = ErrorRaisingArgumentParser()
         subparsers = parser.add_subparsers(dest='command')
         subparsers.add_parser('run')
-        self._test_required_subparsers(parser)
+        # No error here
+        ret = parser.parse_args(())
+        self.assertIsNone(ret.command)
 
     def test_optional_subparsers(self):
         parser = ErrorRaisingArgumentParser()
index 190038d5dd12e09733c21a660c8af2c5c728fd7b..621de6d78a76d2389711b320d57fe2de15990a83 100644 (file)
@@ -477,6 +477,8 @@ module now requires sqlite version at least 3.3.9.
 argparse subparsers are now required by default.  This matches behaviour in
 Python 2. For optional subparsers, use the new parameter
 ``add_subparsers(required=False)``. Patch by Anthony Sottile.
+(As of 3.7.0rc1, the default was changed to not required as had been the case
+since Python 3.3.)
 
 ..
 
diff --git a/Misc/NEWS.d/next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst b/Misc/NEWS.d/next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst
new file mode 100644 (file)
index 0000000..be731f9
--- /dev/null
@@ -0,0 +1,2 @@
+argparse subparsers are once again not required by default, reverting the
+change in behavior introduced by bpo-26510 in 3.7.0a2.