Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-83901: Improve Signature.bind error message for missing keyword-only params #95347

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3114,8 +3114,12 @@ def _bind(self, args, kwargs, *, partial=False):
parameters_ex = (param,)
break
else:
msg = 'missing a required argument: {arg!r}'
msg = msg.format(arg=param.name)
if param.kind == _KEYWORD_ONLY:
argtype = ' keyword-only'
else:
argtype = ''
msg = 'missing a required{argtype} argument: {arg!r}'
msg = msg.format(arg=param.name, argtype=argtype)
raise TypeError(msg) from None
else:
# We have a positional argument to process
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3891,7 +3891,8 @@ def test(foo, *, bar):
self.call(test, 1, bar=2, spam='ham')

with self.assertRaisesRegex(TypeError,
"missing a required argument: 'bar'"):
"missing a required keyword-only "
"argument: 'bar'"):
self.call(test, 1)

def test(foo, *, bar, **bin):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve :meth:`Signature.bind <inspect.Signature.bind>` error message for missing keyword-only arguments.