Skip to content

Commit dd7a255

Browse files
bpo-33109: argparse subparsers are once again not required by default (GH-6919) (GH-7089)
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 8ebf5ce) Co-authored-by: Ned Deily <nad@python.org>
1 parent fa286ed commit dd7a255

File tree

5 files changed

+9
-3
lines changed

5 files changed

+9
-3
lines changed

Doc/library/argparse.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ Sub-commands
15771577
stored; by default ``None`` and no value is stored
15781578

15791579
* required_ - Whether or not a subcommand must be provided, by default
1580-
``True``.
1580+
``False``.
15811581

15821582
* help_ - help for sub-parser group in help output, by default ``None``
15831583

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ def __init__(self,
10771077
prog,
10781078
parser_class,
10791079
dest=SUPPRESS,
1080-
required=True,
1080+
required=False,
10811081
help=None,
10821082
metavar=None):
10831083

Lib/test/test_argparse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,9 @@ def test_required_subparsers_default(self):
19321932
parser = ErrorRaisingArgumentParser()
19331933
subparsers = parser.add_subparsers(dest='command')
19341934
subparsers.add_parser('run')
1935-
self._test_required_subparsers(parser)
1935+
# No error here
1936+
ret = parser.parse_args(())
1937+
self.assertIsNone(ret.command)
19361938

19371939
def test_optional_subparsers(self):
19381940
parser = ErrorRaisingArgumentParser()

Misc/NEWS.d/3.7.0a2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ module now requires sqlite version at least 3.3.9.
477477
argparse subparsers are now required by default. This matches behaviour in
478478
Python 2. For optional subparsers, use the new parameter
479479
``add_subparsers(required=False)``. Patch by Anthony Sottile.
480+
(As of 3.7.0rc1, the default was changed to not required as had been the case
481+
since Python 3.3.)
480482

481483
..
482484
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
argparse subparsers are once again not required by default, reverting the
2+
change in behavior introduced by bpo-26510 in 3.7.0a2.

0 commit comments

Comments
 (0)