From cbea1d10e0133201e6c215c7f2697bac44eb33dc Mon Sep 17 00:00:00 2001 From: Ned Deily Date: Wed, 16 May 2018 15:27:48 -0400 Subject: [PATCH] bpo-33109: argparse subparsers are once again not required by default 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. --- Doc/library/argparse.rst | 2 +- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 4 +++- Misc/NEWS.d/3.7.0a2.rst | 2 ++ .../next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst | 2 ++ 5 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 3f327c0cfa77b7..260e5cbf700035 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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`` diff --git a/Lib/argparse.py b/Lib/argparse.py index e3da7f0443cfa2..e0e367bf20ca96 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1077,7 +1077,7 @@ def __init__(self, prog, parser_class, dest=SUPPRESS, - required=True, + required=False, help=None, metavar=None): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index c4440e4df7c15b..bcf15ce123e463 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1932,7 +1932,9 @@ def test_required_subparsers_default(self): 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() diff --git a/Misc/NEWS.d/3.7.0a2.rst b/Misc/NEWS.d/3.7.0a2.rst index 190038d5dd12e0..621de6d78a76d2 100644 --- a/Misc/NEWS.d/3.7.0a2.rst +++ b/Misc/NEWS.d/3.7.0a2.rst @@ -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 index 00000000000000..be731f99f7f090 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-05-16-14-57-58.bpo-33109.nPLL_S.rst @@ -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.