[Python-checkins] GH-83901: Improve Signature.bind error message for missing keyword-only params (#95347)

JelleZijlstra webhook-mailer at python.org
Fri Oct 7 18:24:26 EDT 2022


https://github.com/python/cpython/commit/f4f813338736aecc9285a9408cb8a07eaeb0e373
commit: f4f813338736aecc9285a9408cb8a07eaeb0e373
branch: main
author: Frazer McLean <frazer at frazermclean.co.uk>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-10-07T15:24:17-07:00
summary:

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

Fixes GH-83901

files:
A Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst
M Lib/inspect.py
M Lib/test/test_inspect.py

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 @@ 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 61fed323dcea..cfc6e411ea68 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 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 <inspect.Signature.bind>` error message for missing keyword-only arguments.



More information about the Python-checkins mailing list