diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-19 04:27:49 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-19 04:27:49 (GMT) |
commit | 8687275358a9267c5ea7dcd4ce6a4cbd0f91f541 (patch) | |
tree | 7d93ab90e1f03c10dd6df80933bbace0b7a155ff | |
parent | fee05daef8b9e0e0977e413260009ebf54bf4486 (diff) | |
download | cpython-8687275358a9267c5ea7dcd4ce6a4cbd0f91f541.zip cpython-8687275358a9267c5ea7dcd4ce6a4cbd0f91f541.tar.gz cpython-8687275358a9267c5ea7dcd4ce6a4cbd0f91f541.tar.bz2 |
Issue 24205: Improve inspect.Signature.bind() error messages.
-rw-r--r-- | Lib/inspect.py | 16 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 30 |
2 files changed, 29 insertions, 17 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 8d2920a..13d0c7b 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2762,7 +2762,7 @@ class Signature: 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 @@ class Signature: 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 @@ class Signature: 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 @@ class Signature: # 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 @@ class Signature: # 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 index e712efb..92bc096 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -2891,7 +2891,9 @@ class TestSignatureBind(unittest.TestCase): 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 @@ class TestSignatureBind(unittest.TestCase): 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 @@ class TestSignatureBind(unittest.TestCase): 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 @@ class TestSignatureBind(unittest.TestCase): 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 @@ class TestSignatureBind(unittest.TestCase): 'too many positional arguments'): self.call(test, 1, 2, bar=2) - with self.assertRaisesRegex(TypeError, - 'too many keyword arguments'): + with self.assertRaisesRegex( + TypeError, "got an unexpected keyword argument 'spam'"): + 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 @@ class TestSignatureBind(unittest.TestCase): 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 @@ class TestSignatureBind(unittest.TestCase): 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): |