From: Frazer McLean Date: Fri, 7 Oct 2022 22:24:17 +0000 (+0200) Subject: GH-83901: Improve Signature.bind error message for missing keyword-only params (... X-Git-Tag: v3.12.0a1~168 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f4f813338736aecc9285a9408cb8a07eaeb0e373;p=thirdparty%2FPython%2Fcpython.git GH-83901: Improve Signature.bind error message for missing keyword-only params (#95347) Fixes GH-83901 --- diff --git a/Lib/inspect.py b/Lib/inspect.py index 585875a30c35..f6750c3b211f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -3102,8 +3102,12 @@ class Signature: 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 61fed323dcea..cfc6e411ea68 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3898,7 +3898,8 @@ class TestSignatureBind(unittest.TestCase): 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 000000000000..da407905a886 --- /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.