[Python-checkins] bpo-16970: Adding error message for invalid args (GH-14844)

Miss Islington (bot) webhook-mailer at python.org
Fri Aug 2 00:57:31 EDT 2019


https://github.com/python/cpython/commit/4b3e97592376d5f8a3b75192b399a2da1be642cb
commit: 4b3e97592376d5f8a3b75192b399a2da1be642cb
branch: master
author: tmblweed <tmblweed at users.noreply.github.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-08-01T21:57:13-07:00
summary:

bpo-16970: Adding error message for invalid args (GH-14844)



BPO -16970: Adding error message for invalid args

Applied the patch argparse-v2 patch issue 16970, ran patch check and the test suite, test_argparse with 0 errors


https://bugs.python.org/issue16970

files:
A Misc/NEWS.d/next/Library/2019-07-19-01-46-56.bpo-16970.GEASf5.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
M Misc/ACKS

diff --git a/Lib/argparse.py b/Lib/argparse.py
index e45b67b67704..a300828f9e3d 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -593,7 +593,10 @@ def _format_args(self, action, default_metavar):
         elif action.nargs == SUPPRESS:
             result = ''
         else:
-            formats = ['%s' for _ in range(action.nargs)]
+            try:
+                formats = ['%s' for _ in range(action.nargs)]
+            except TypeError:
+                raise ValueError("invalid nargs value") from None
             result = ' '.join(formats) % get_metavar(action.nargs)
         return result
 
@@ -850,7 +853,7 @@ def __init__(self,
                  help=None,
                  metavar=None):
         if nargs == 0:
-            raise ValueError('nargs for store actions must be > 0; if you '
+            raise ValueError('nargs for store actions must be != 0; if you '
                              'have nothing to store, actions such as store '
                              'true or store const may be more appropriate')
         if const is not None and nargs != OPTIONAL:
@@ -942,7 +945,7 @@ def __init__(self,
                  help=None,
                  metavar=None):
         if nargs == 0:
-            raise ValueError('nargs for append actions must be > 0; if arg '
+            raise ValueError('nargs for append actions must be != 0; if arg '
                              'strings are not supplying the value to append, '
                              'the append const action may be more appropriate')
         if const is not None and nargs != OPTIONAL:
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 5128dc5e1be4..d6d16090eb02 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4263,7 +4263,6 @@ class TestHelpSubparsersWithHelpOrdering(HelpTestCase):
 
 
 class TestHelpMetavarTypeFormatter(HelpTestCase):
-    """"""
 
     def custom_type(string):
         return string
@@ -5150,6 +5149,35 @@ def test_nargs_3_metavar_length2(self):
     def test_nargs_3_metavar_length3(self):
         self.do_test_no_exception(nargs=3, metavar=("1", "2", "3"))
 
+
+class TestInvalidNargs(TestCase):
+
+    EXPECTED_INVALID_MESSAGE = "invalid nargs value"
+    EXPECTED_RANGE_MESSAGE = ("nargs for store actions must be != 0; if you "
+                              "have nothing to store, actions such as store "
+                              "true or store const may be more appropriate")
+
+    def do_test_range_exception(self, nargs):
+        parser = argparse.ArgumentParser()
+        with self.assertRaises(ValueError) as cm:
+            parser.add_argument("--foo", nargs=nargs)
+        self.assertEqual(cm.exception.args[0], self.EXPECTED_RANGE_MESSAGE)
+
+    def do_test_invalid_exception(self, nargs):
+        parser = argparse.ArgumentParser()
+        with self.assertRaises(ValueError) as cm:
+            parser.add_argument("--foo", nargs=nargs)
+        self.assertEqual(cm.exception.args[0], self.EXPECTED_INVALID_MESSAGE)
+
+    # Unit tests for different values of nargs
+
+    def test_nargs_alphabetic(self):
+        self.do_test_invalid_exception(nargs='a')
+        self.do_test_invalid_exception(nargs="abcd")
+
+    def test_nargs_zero(self):
+        self.do_test_range_exception(nargs=0)
+
 # ============================
 # from argparse import * tests
 # ============================
diff --git a/Misc/ACKS b/Misc/ACKS
index 6463b535e17c..82fc51c98c40 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1878,3 +1878,4 @@ Edison Abahurire
 Geoff Shannon
 Batuhan Taskaya
 Aleksandr Balezin
+Robert Leenders
diff --git a/Misc/NEWS.d/next/Library/2019-07-19-01-46-56.bpo-16970.GEASf5.rst b/Misc/NEWS.d/next/Library/2019-07-19-01-46-56.bpo-16970.GEASf5.rst
new file mode 100644
index 000000000000..7285b8176032
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-07-19-01-46-56.bpo-16970.GEASf5.rst
@@ -0,0 +1,2 @@
+Adding a value error when an invalid value in passed to nargs
+Patch by Robert Leenders



More information about the Python-checkins mailing list