Skip to content

Commit d6931c5

Browse files
jacobtylerwallsmiss-islington
authored andcommitted
pythongh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults (pythonGH-130192)
Follow-up to 9c15202. (cherry picked from commit dab456d) Co-authored-by: Jacob Walls <[email protected]>
1 parent 57e8b0c commit d6931c5

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/inspect.py

+3
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,9 @@ def _bind(self, args, kwargs, *, partial=False):
31483148
break
31493149
elif param.name in kwargs:
31503150
if param.kind == _POSITIONAL_ONLY:
3151+
if param.default is _empty:
3152+
msg = f'missing a required positional-only argument: {param.name!r}'
3153+
raise TypeError(msg)
31513154
# Raise a TypeError once we are sure there is no
31523155
# **kwargs param later.
31533156
pos_only_param_in_kwargs.append(param)

Lib/test/test_inspect/test_inspect.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5279,7 +5279,11 @@ class TestSignatureBind(unittest.TestCase):
52795279
def call(func, *args, **kwargs):
52805280
sig = inspect.signature(func)
52815281
ba = sig.bind(*args, **kwargs)
5282-
return func(*ba.args, **ba.kwargs)
5282+
# Prevent unexpected success of assertRaises(TypeError, ...)
5283+
try:
5284+
return func(*ba.args, **ba.kwargs)
5285+
except TypeError as e:
5286+
raise AssertionError from e
52835287

52845288
def test_signature_bind_empty(self):
52855289
def test():
@@ -5479,7 +5483,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
54795483
self.assertEqual(self.call(test, 1, 2, c_po=4),
54805484
(1, 2, 3, 42, 50, {'c_po': 4}))
54815485

5482-
with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
5486+
with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):
54835487
self.call(test, a_po=1, b_po=2)
54845488

54855489
def without_var_kwargs(c_po=3, d_po=4, /):
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 arguments provided by keyword when a variadic keyword
3+
argument (e.g. ``**kwargs``) is present.

0 commit comments

Comments
 (0)