Skip to content

Commit f4f8133

Browse files
authored
GH-83901: Improve Signature.bind error message for missing keyword-only params (#95347)
Fixes GH-83901
1 parent 5eaf4d6 commit f4f8133

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Lib/inspect.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,8 +3102,12 @@ def _bind(self, args, kwargs, *, partial=False):
31023102
parameters_ex = (param,)
31033103
break
31043104
else:
3105-
msg = 'missing a required argument: {arg!r}'
3106-
msg = msg.format(arg=param.name)
3105+
if param.kind == _KEYWORD_ONLY:
3106+
argtype = ' keyword-only'
3107+
else:
3108+
argtype = ''
3109+
msg = 'missing a required{argtype} argument: {arg!r}'
3110+
msg = msg.format(arg=param.name, argtype=argtype)
31073111
raise TypeError(msg) from None
31083112
else:
31093113
# We have a positional argument to process

Lib/test/test_inspect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3898,7 +3898,8 @@ def test(foo, *, bar):
38983898
self.call(test, 1, bar=2, spam='ham')
38993899

39003900
with self.assertRaisesRegex(TypeError,
3901-
"missing a required argument: 'bar'"):
3901+
"missing a required keyword-only "
3902+
"argument: 'bar'"):
39023903
self.call(test, 1)
39033904

39043905
def test(foo, *, bar, **bin):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve :meth:`Signature.bind <inspect.Signature.bind>` error message for missing keyword-only arguments.

0 commit comments

Comments
 (0)