Skip to content

Commit

Permalink
pythonGH-83901: Improve Signature.bind error message for missing keyw…
Browse files Browse the repository at this point in the history
…ord-only params (python#95347)

Fixes pythonGH-83901
  • Loading branch information
RazerM authored and mpage committed Oct 11, 2022
1 parent 864f9b9 commit c437b6e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3102,8 +3102,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 @@ -3898,7 +3898,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.

0 comments on commit c437b6e

Please sign in to comment.