]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-83901: Improve Signature.bind error message for missing keyword-only params (...
authorFrazer McLean <frazer@frazermclean.co.uk>
Fri, 7 Oct 2022 22:24:17 +0000 (00:24 +0200)
committerGitHub <noreply@github.com>
Fri, 7 Oct 2022 22:24:17 +0000 (15:24 -0700)
Fixes GH-83901

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2022-07-27-19-47-51.gh-issue-83901.OSw06c.rst [new file with mode: 0644]

index 585875a30c35c3b4946c7ef3b0fecc0f00d986c1..f6750c3b211fbd000b39c3d9596098fe8cb96353 100644 (file)
@@ -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
index 61fed323dceae49302842618d6374c742e68e13c..cfc6e411ea680dc8d05e67ec7d603127c1ed6fe4 100644 (file)
@@ -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 (file)
index 0000000..da40790
--- /dev/null
@@ -0,0 +1 @@
+Improve :meth:`Signature.bind <inspect.Signature.bind>` error message for missing keyword-only arguments.