Skip to content

Commit 853f922

Browse files
committed
Improve Signature.bind error message for missing keyword-only params
Fixes pythonGH-83901
1 parent f40bc7f commit 853f922

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Lib/inspect.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3114,8 +3114,12 @@ def _bind(self, args, kwargs, *, partial=False):
31143114
parameters_ex = (param,)
31153115
break
31163116
else:
3117-
msg = 'missing a required argument: {arg!r}'
3118-
msg = msg.format(arg=param.name)
3117+
if param.kind == _KEYWORD_ONLY:
3118+
argtype = ' keyword-only'
3119+
else:
3120+
argtype = ''
3121+
msg = 'missing a required{argtype} argument: {arg!r}'
3122+
msg = msg.format(arg=param.name, argtype=argtype)
31193123
raise TypeError(msg) from None
31203124
else:
31213125
# We have a positional argument to process

Lib/test/test_inspect.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3891,7 +3891,8 @@ def test(foo, *, bar):
38913891
self.call(test, 1, bar=2, spam='ham')
38923892

38933893
with self.assertRaisesRegex(TypeError,
3894-
"missing a required argument: 'bar'"):
3894+
"missing a required keyword-only "
3895+
"argument: 'bar'"):
38953896
self.call(test, 1)
38963897

38973898
def test(foo, *, bar, **bin):

0 commit comments

Comments
 (0)