[Python-checkins] bpo-29298: Fix crash with required subparsers without dest (GH-3680)

miss-islington webhook-mailer at python.org
Fri Jul 23 08:49:25 EDT 2021


https://github.com/python/cpython/commit/17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c
commit: 17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c
branch: main
author: Anthony Sottile <asottile at umich.edu>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-07-23T05:49:04-07:00
summary:

bpo-29298: Fix crash with required subparsers without dest (GH-3680)



Automerge-Triggered-By: GH:encukou

files:
A Misc/NEWS.d/next/Library/2017-09-20-14-43-03.bpo-29298._78CSN.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 8a12dea766879..e3a49e74fa8d1 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -727,6 +727,8 @@ def _get_action_name(argument):
         return argument.metavar
     elif argument.dest not in (None, SUPPRESS):
         return argument.dest
+    elif argument.choices:
+        return '{' + ','.join(argument.choices) + '}'
     else:
         return None
 
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 4d0316f73edcd..0994e70e65e1c 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2060,6 +2060,30 @@ def test_required_subparsers_default(self):
         ret = parser.parse_args(())
         self.assertIsNone(ret.command)
 
+    def test_required_subparsers_no_destination_error(self):
+        parser = ErrorRaisingArgumentParser()
+        subparsers = parser.add_subparsers(required=True)
+        subparsers.add_parser('foo')
+        subparsers.add_parser('bar')
+        with self.assertRaises(ArgumentParserError) as excinfo:
+            parser.parse_args(())
+        self.assertRegex(
+            excinfo.exception.stderr,
+            'error: the following arguments are required: {foo,bar}\n$'
+        )
+
+    def test_wrong_argument_subparsers_no_destination_error(self):
+        parser = ErrorRaisingArgumentParser()
+        subparsers = parser.add_subparsers(required=True)
+        subparsers.add_parser('foo')
+        subparsers.add_parser('bar')
+        with self.assertRaises(ArgumentParserError) as excinfo:
+            parser.parse_args(('baz',))
+        self.assertRegex(
+            excinfo.exception.stderr,
+            r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
+        )
+
     def test_optional_subparsers(self):
         parser = ErrorRaisingArgumentParser()
         subparsers = parser.add_subparsers(dest='command', required=False)
diff --git a/Misc/NEWS.d/next/Library/2017-09-20-14-43-03.bpo-29298._78CSN.rst b/Misc/NEWS.d/next/Library/2017-09-20-14-43-03.bpo-29298._78CSN.rst
new file mode 100644
index 0000000000000..e84c6de02cd27
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-20-14-43-03.bpo-29298._78CSN.rst
@@ -0,0 +1,2 @@
+Fix ``TypeError`` when required subparsers without ``dest`` do not receive
+arguments. Patch by Anthony Sottile.



More information about the Python-checkins mailing list