]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] 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:08:06 +0000 (11:08 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Apr 2025 09:08:06 +0000 (09:08 +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 81be29a33b577db095077c572c2c4f69ed34a0e2..401a76391aa1e2df6823a712408c76998a5714df 100644 (file)
@@ -3150,6 +3150,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 a88f1713210e1b8abb7f020e05f9ca7e8ae9594a..5e46f4fe61b55959a636b328656d272b20c94c1d 100644 (file)
@@ -5300,7 +5300,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():
@@ -5500,7 +5504,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.