[Python-checkins] cpython: Issue 24205: Improve inspect.Signature.bind() error messages.

yury.selivanov python-checkins at python.org
Tue May 19 06:27:54 CEST 2015


https://hg.python.org/cpython/rev/3cc368d334c5
changeset:   96150:3cc368d334c5
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Tue May 19 00:27:49 2015 -0400
summary:
  Issue 24205: Improve inspect.Signature.bind() error messages.

files:
  Lib/inspect.py           |  16 ++++++++----
  Lib/test/test_inspect.py |  34 +++++++++++++++++----------
  2 files changed, 31 insertions(+), 19 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2762,7 +2762,7 @@
                             parameters_ex = (param,)
                             break
                         else:
-                            msg = '{arg!r} parameter lacking default value'
+                            msg = 'missing a required argument: {arg!r}'
                             msg = msg.format(arg=param.name)
                             raise TypeError(msg) from None
             else:
@@ -2775,7 +2775,8 @@
                     if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
                         # Looks like we have no parameter for this positional
                         # argument
-                        raise TypeError('too many positional arguments')
+                        raise TypeError(
+                            'too many positional arguments') from None
 
                     if param.kind == _VAR_POSITIONAL:
                         # We have an '*args'-like argument, let's fill it with
@@ -2787,8 +2788,9 @@
                         break
 
                     if param.name in kwargs:
-                        raise TypeError('multiple values for argument '
-                                        '{arg!r}'.format(arg=param.name))
+                        raise TypeError(
+                            'multiple values for argument {arg!r}'.format(
+                                arg=param.name)) from None
 
                     arguments[param.name] = arg_val
 
@@ -2817,7 +2819,7 @@
                 # arguments.
                 if (not partial and param.kind != _VAR_POSITIONAL and
                                                     param.default is _empty):
-                    raise TypeError('{arg!r} parameter lacking default value'. \
+                    raise TypeError('missing a required argument: {arg!r}'. \
                                     format(arg=param_name)) from None
 
             else:
@@ -2836,7 +2838,9 @@
                 # Process our '**kwargs'-like parameter
                 arguments[kwargs_param.name] = kwargs
             else:
-                raise TypeError('too many keyword arguments')
+                raise TypeError(
+                    'got an unexpected keyword argument {arg!r}'.format(
+                        arg=next(iter(kwargs))))
 
         return self._bound_arguments_cls(self, arguments)
 
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -2891,7 +2891,9 @@
             self.call(test, 1)
         with self.assertRaisesRegex(TypeError, 'too many positional arguments'):
             self.call(test, 1, spam=10)
-        with self.assertRaisesRegex(TypeError, 'too many keyword arguments'):
+        with self.assertRaisesRegex(
+            TypeError, "got an unexpected keyword argument 'spam'"):
+
             self.call(test, spam=1)
 
     def test_signature_bind_var(self):
@@ -2916,10 +2918,12 @@
         with self.assertRaisesRegex(TypeError, 'too many positional arguments'):
             self.call(test, 1, 2, 3, 4)
 
-        with self.assertRaisesRegex(TypeError, "'b' parameter lacking default"):
+        with self.assertRaisesRegex(TypeError,
+                                    "missing a required argument: 'b'"):
             self.call(test, 1)
 
-        with self.assertRaisesRegex(TypeError, "'a' parameter lacking default"):
+        with self.assertRaisesRegex(TypeError,
+                                    "missing a required argument: 'a'"):
             self.call(test)
 
         def test(a, b, c=10):
@@ -2992,7 +2996,7 @@
         def test(a, *, foo=1, bar):
             return foo
         with self.assertRaisesRegex(TypeError,
-                                     "'bar' parameter lacking default value"):
+                                     "missing a required argument: 'bar'"):
             self.call(test, 1)
 
         def test(foo, *, bar):
@@ -3000,8 +3004,9 @@
         self.assertEqual(self.call(test, 1, bar=2), (1, 2))
         self.assertEqual(self.call(test, bar=2, foo=1), (1, 2))
 
-        with self.assertRaisesRegex(TypeError,
-                                     'too many keyword arguments'):
+        with self.assertRaisesRegex(
+            TypeError, "got an unexpected keyword argument 'spam'"):
+
             self.call(test, bar=2, foo=1, spam=10)
 
         with self.assertRaisesRegex(TypeError,
@@ -3012,12 +3017,13 @@
                                      'too many positional arguments'):
             self.call(test, 1, 2, bar=2)
 
+        with self.assertRaisesRegex(
+            TypeError, "got an unexpected keyword argument 'spam'"):
+
+            self.call(test, 1, bar=2, spam='ham')
+
         with self.assertRaisesRegex(TypeError,
-                                     'too many keyword arguments'):
-            self.call(test, 1, bar=2, spam='ham')
-
-        with self.assertRaisesRegex(TypeError,
-                                     "'bar' parameter lacking default value"):
+                                     "missing a required argument: 'bar'"):
             self.call(test, 1)
 
         def test(foo, *, bar, **bin):
@@ -3029,7 +3035,7 @@
         self.assertEqual(self.call(test, spam='ham', foo=1, bar=2),
                          (1, 2, {'spam': 'ham'}))
         with self.assertRaisesRegex(TypeError,
-                                     "'foo' parameter lacking default value"):
+                                    "missing a required argument: 'foo'"):
             self.call(test, spam='ham', bar=2)
         self.assertEqual(self.call(test, 1, bar=2, bin=1, spam=10),
                          (1, 2, {'bin': 1, 'spam': 10}))
@@ -3094,7 +3100,9 @@
             return a, args
         sig = inspect.signature(test)
 
-        with self.assertRaisesRegex(TypeError, "too many keyword arguments"):
+        with self.assertRaisesRegex(
+            TypeError, "got an unexpected keyword argument 'args'"):
+
             sig.bind(a=0, args=1)
 
         def test(*args, **kwargs):

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list