8000 [3.13] gh-130164: Fix inspect.Signature.bind() handling of positional… · python/cpython@5c658c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c658c1

Browse files
[3.13] gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults (GH-130192) (GH-130271)
Follow-up to 9c15202. (cherry picked from commit dab456d) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent 5aa7aeb commit 5c658c1

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/inspect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,9 @@ def _bind(self, args, kwargs, *, partial=False):
31503150
break
31513151
elif param.name in kwargs:
31523152
if param.kind == _POSITIONAL_ONLY:
3153+
if param.default is _empty:
3154+
msg = f'missing a required positional-only argument: {param.name!r}'
3155+
raise TypeError(msg)
31533156
# Raise a TypeError once we are sure there is no
31543157
# **kwargs param later.
31553158
pos_only_param_in_kwargs.append(param)

Lib/test/test_inspect/test_inspect.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5300,7 +5300,11 @@ class TestSignatureBind(unittest.TestCase):
53005300
def call(func, *args, **kwargs):
53015301
sig = inspect.signature(func)
53025302
ba = sig.bind(*args, **kwargs)
5303-
return func(*ba.args, **ba.kwargs)
5303+
# Prevent unexpected success of assertRaises(TypeError, ...)
5304+
try:
5305+
return func(*ba.args, **ba.kwargs)
5306+
except TypeError as e:
5307+
raise AssertionError from e
53045308

53055309
def test_signature_bind_empty(self):
53065310
def test():
@@ -5500,7 +5504,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
55005504
self.assertEqual(self.call(test, 1, 2, c_po=4),
55015505
(1, 2, 3, 42, 50, {'c_po': 4}))
55025506

5503-
with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
5507+
with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):
55045508
self.call(test, a_po=1, b_po=2)
55055509

55065510
def without_var_kwargs(c_po=3, d_po=4, /):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed failure to raise :exc:`TypeError` in :meth:`inspect.Signature.bind`
2+
for positional-only 3B21 arguments provided by keyword when a variadic keyword
3+
argument (e.g. ``**kwargs``) is present.

0 commit comments

Comments
 (0)
0