From 853f92263fb906841511d3971679c94ce23edb02 Mon Sep 17 00:00:00 2001 From: Frazer McLean Date: Wed, 27 Jul 2022 20:43:46 +0100 Subject: [PATCH 1/2] Improve Signature.bind error message for missing keyword-only params Fixes GH-83901 --- Lib/inspect.py | 8 ++++++-- Lib/test/test_inspect.py | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index cbc0632484b832..a1cd53a594a661 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index be9f29e04ae110..3df9f42eb43867 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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): From 536734c675f1def479d303bf6dc3ba3f37bc5002 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 27 Jul 2022 19:47:52 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst 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 00000000000000..da407905a886fd --- /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.