]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-130164: Fix inspect.Signature.bind() handling of positional-only args witho...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 8 Apr 2025 09:39:45 +0000 (11:39 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Apr 2025 09:39:45 +0000 (09:39 +0000)
Follow-up to 9c15202.
(cherry picked from commit dab456dcefd886bde44eb204dc6f1b2f14de0e9d)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Lib/inspect.py
Lib/test/test_inspect/test_inspect.py
Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst [new file with mode: 0644]

index b630cb2835957b5a8bbdcac1f1b94aa9e379701f..2a8d9b053cc5d1d0b9a5c9948fc743b4479b3743 100644 (file)
@@ -3161,6 +3161,9 @@ class Signature:
                         break
                     elif param.name in kwargs:
                         if param.kind == _POSITIONAL_ONLY:
+                            if param.default is _empty:
+                                msg = f'missing a required positional-only argument: {param.name!r}'
+                                raise TypeError(msg)
                             # Raise a TypeError once we are sure there is no
                             # **kwargs param later.
                             pos_only_param_in_kwargs.append(param)
index eae8e46b9cb9b2021a1289cba0047d5a4457d31c..792a8fa90f593c68c5a15bed1172f1ed1151b02b 100644 (file)
@@ -4701,7 +4701,11 @@ class TestSignatureBind(unittest.TestCase):
     def call(func, *args, **kwargs):
         sig = inspect.signature(func)
         ba = sig.bind(*args, **kwargs)
-        return func(*ba.args, **ba.kwargs)
+        # Prevent unexpected success of assertRaises(TypeError, ...)
+        try:
+            return func(*ba.args, **ba.kwargs)
+        except TypeError as e:
+            raise AssertionError from e
 
     def test_signature_bind_empty(self):
         def test():
@@ -4901,7 +4905,7 @@ class TestSignatureBind(unittest.TestCase):
         self.assertEqual(self.call(test, 1, 2, c_po=4),
                          (1, 2, 3, 42, 50, {'c_po': 4}))
 
-        with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
+        with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):
             self.call(test, a_po=1, b_po=2)
 
         def without_var_kwargs(c_po=3, d_po=4, /):
diff --git a/Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst b/Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst
new file mode 100644 (file)
index 0000000..a4a47cb
--- /dev/null
@@ -0,0 +1,3 @@
+Fixed failure to raise :exc:`TypeError` in :meth:`inspect.Signature.bind`
+for positional-only arguments provided by keyword when a variadic keyword
+argument (e.g. ``**kwargs``) is present.