Skip to content

Commit c589992

Browse files
bpo-29298: Fix crash with required subparsers without dest (GH-3680) (GH-27303)
(cherry picked from commit 17575f7) Co-authored-by: Anthony Sottile <[email protected]>
1 parent 21db59f commit c589992

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/argparse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ def _get_action_name(argument):
727727
return argument.metavar
728728
elif argument.dest not in (None, SUPPRESS):
729729
return argument.dest
730+
elif argument.choices:
731+
return '{' + ','.join(argument.choices) + '}'
730732
else:
731733
return None
732734

Lib/test/test_argparse.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,30 @@ def test_required_subparsers_default(self):
20602060
ret = parser.parse_args(())
20612061
self.assertIsNone(ret.command)
20622062

2063+
def test_required_subparsers_no_destination_error(self):
2064+
parser = ErrorRaisingArgumentParser()
2065+
subparsers = parser.add_subparsers(required=True)
2066+
subparsers.add_parser('foo')
2067+
subparsers.add_parser('bar')
2068+
with self.assertRaises(ArgumentParserError) as excinfo:
2069+
parser.parse_args(())
2070+
self.assertRegex(
2071+
excinfo.exception.stderr,
2072+
'error: the following arguments are required: {foo,bar}\n$'
2073+
)
2074+
2075+
def test_wrong_argument_subparsers_no_destination_error(self):
2076+
parser = ErrorRaisingArgumentParser()
2077+
subparsers = parser.add_subparsers(required=True)
2078+
subparsers.add_parser('foo')
2079+
subparsers.add_parser('bar')
2080+
with self.assertRaises(ArgumentParserError) as excinfo:
2081+
parser.parse_args(('baz',))
2082+
self.assertRegex(
2083+
excinfo.exception.stderr,
2084+
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
2085+
)
2086+
20632087
def test_optional_subparsers(self):
20642088
parser = ErrorRaisingArgumentParser()
20652089
subparsers = parser.add_subparsers(dest='command', required=False)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``TypeError`` when required subparsers without ``dest`` do not receive
2+
arguments. Patch by Anthony Sottile.

0 commit comments

Comments
 (0)