bpo-33109: argparse subparsers are once again not required by default… · pythoncapi/cpython@8ebf5ce · GitHub
Skip to content

Commit 8ebf5ce

Browse files
authored
bpo-33109: argparse subparsers are once again not required by default (pythonGH-6919)
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.
1 parent 453bd0b commit 8ebf5ce

5 files changed

Lines changed: 9 additions & 3 deletions

File tree

Doc/library/argparse.rst

Lines changed: 1 addition & 1 deletion

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

0 commit comments

Comments
 (0)