From f4f813338736aecc9285a9408cb8a07eaeb0e373 Mon Sep 17 00:00:00 2001 From: Frazer McLean Date: Sat, 8 Oct 2022 00:24:17 +0200 Subject: [PATCH] GH-83901: Improve Signature.bind error message for missing keyword-only params (#95347) Fixes GH-83901 --- Lib/inspect.py | 8 ++++++-- Lib/test/test_inspect.py | 3 ++- .../Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 585875a30c3..f6750c3b211 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 61fed323dce..cfc6e411ea6 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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): diff --git a/Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst b/Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst new file mode 100644 index 00000000000..da407905a88 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst @@ -0,0 +1 @@ +Improve :meth:`Signature.bind ` error message for missing keyword-only arguments.