Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8687275

Browse files
committed
Issue 24205: Improve inspect.Signature.bind() error messages.
1 parent fee05da commit 8687275

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

Lib/inspect.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ def _bind(self, args, kwargs, *, partial=False):
27622762
parameters_ex = (param,)
27632763
break
27642764
else:
2765-
msg = '{arg!r} parameter lacking default value'
2765+
msg = 'missing a required argument: {arg!r}'
27662766
msg = msg.format(arg=param.name)
27672767
raise TypeError(msg) from None
27682768
else:
@@ -2775,7 +2775,8 @@ def _bind(self, args, kwargs, *, partial=False):
27752775
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
27762776
# Looks like we have no parameter for this positional
27772777
# argument
2778-
raise TypeError('too many positional arguments')
2778+
raise TypeError(
2779+
'too many positional arguments') from None
27792780

27802781
if param.kind == _VAR_POSITIONAL:
27812782
# We have an '*args'-like argument, let's fill it with
@@ -2787,8 +2788,9 @@ def _bind(self, args, kwargs, *, partial=False):
27872788
break
27882789

27892790
if param.name in kwargs:
2790-
raise TypeError('multiple values for argument '
2791-
'{arg!r}'.format(arg=param.name))
2791+
raise TypeError(
2792+
'multiple values for argument {arg!r}'.format(
2793+
arg=param.name)) from None
27922794

27932795
arguments[param.name] = arg_val
27942796

@@ -2817,7 +2819,7 @@ def _bind(self, args, kwargs, *, partial=False):
28172819
# arguments.
28182820
if (not partial and param.kind != _VAR_POSITIONAL and
28192821
param.default is _empty):
2820-
raise TypeError('{arg!r} parameter lacking default value'. \
2822+
raise TypeError('missing a required argument: {arg!r}'. \
28212823
format(arg=param_name)) from None
28222824

28232825
else:
@@ -2836,7 +2838,9 @@ def _bind(self, args, kwargs, *, partial=False):
28362838
# Process our '**kwargs'-like parameter
28372839
arguments[kwargs_param.name] = kwargs
28382840
else:
2839-
raise TypeError('too many keyword arguments')
2841+
raise TypeError(
2842+
'got an unexpected keyword argument {arg!r}'.format(
2843+
arg=next(iter(kwargs))))
28402844

28412845
return self._bound_arguments_cls(self, arguments)
28422846

Lib/test/test_inspect.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,9 @@ def test():
28912891
self.call(test, 1)
28922892
with self.assertRaisesRegex(TypeError, 'too many positional arguments'):
28932893
self.call(test, 1, spam=10)
2894-
with self.assertRaisesRegex(TypeError, 'too many keyword arguments'):
2894+
with self.assertRaisesRegex(
2895+
TypeError, "got an unexpected keyword argument 'spam'"):
2896+
28952897
self.call(test, spam=1)
28962898

28972899
def test_signature_bind_var(self):
@@ -2916,10 +2918,12 @@ def test(a, b, c):
29162918
with self.assertRaisesRegex(TypeError, 'too many positional arguments'):
29172919
self.call(test, 1, 2, 3, 4)
29182920

2919-
with self.assertRaisesRegex(TypeError, "'b' parameter lacking default"):
2921+
with self.assertRaisesRegex(TypeError,
2922+
"missing a required argument: 'b'"):
29202923
self.call(test, 1)
29212924

2922-
with self.assertRaisesRegex(TypeError, "'a' parameter lacking default"):
2925+
with self.assertRaisesRegex(TypeError,
2926+
"missing a required argument: 'a'"):
29232927
self.call(test)
29242928

29252929
def test(a, b, c=10):
@@ -2992,16 +2996,17 @@ def test(*, foo):
29922996
def test(a, *, foo=1, bar):
29932997
return foo
29942998
with self.assertRaisesRegex(TypeError,
2995-
"'bar' parameter lacking default value"):
2999+
"missing a required argument: 'bar'"):
29963000
self.call(test, 1)
29973001

29983002
def test(foo, *, bar):
29993003
return foo, bar
30003004
self.assertEqual(self.call(test, 1, bar=2), (1, 2))
30013005
self.assertEqual(self.call(test, bar=2, foo=1), (1, 2))
30023006

3003-
with self.assertRaisesRegex(TypeError,
3004-
'too many keyword arguments'):
3007+
with self.assertRaisesRegex(
3008+
TypeError, "got an unexpected keyword argument 'spam'"):
3009+
30053010
self.call(test, bar=2, foo=1, spam=10)
30063011

30073012
with self.assertRaisesRegex(TypeError,
@@ -3012,12 +3017,13 @@ def test(foo, *, bar):
30123017
'too many positional arguments'):
30133018
self.call(test, 1, 2, bar=2)
30143019

3015-
with self.assertRaisesRegex(TypeError,
3016-
'too many keyword arguments'):
3020+
with self.assertRaisesRegex(
3021+
TypeError, "got an unexpected keyword argument 'spam'"):
3022+
30173023
self.call(test, 1, bar=2, spam='ham')
30183024

30193025
with self.assertRaisesRegex(TypeError,
3020-
"'bar' parameter lacking default value"):
3026+
"missing a required argument: 'bar'"):
30213027
self.call(test, 1)
30223028

30233029
def test(foo, *, bar, **bin):
@@ -3029,7 +3035,7 @@ def test(foo, *, bar, **bin):
30293035
self.assertEqual(self.call(test, spam='ham', foo=1, bar=2),
30303036
(1, 2, {'spam': 'ham'}))
30313037
with self.assertRaisesRegex(TypeError,
3032-
"'foo' parameter lacking default value"):
3038+
"missing a required argument: 'foo'"):
30333039
self.call(test, spam='ham', bar=2)
30343040
self.assertEqual(self.call(test, 1, bar=2, bin=1, spam=10),
30353041
(1, 2, {'bin': 1, 'spam': 10}))
@@ -3094,7 +3100,9 @@ def test(a, *args):
30943100
return a, args
30953101
sig = inspect.signature(test)
30963102

3097-
with self.assertRaisesRegex(TypeError, "too many keyword arguments"):
3103+
with self.assertRaisesRegex(
3104+
TypeError, "got an unexpected keyword argument 'args'"):
3105+
30983106
sig.bind(a=0, args=1)
30993107

31003108
def test(*args, **kwargs):

0 commit comments

Comments
 (0)