Skip to content

Commit dab456d

Browse files
gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults (GH-130192)
Follow-up to 9c15202.
1 parent 01ba7df commit dab456d

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
@@ -3077,6 +3077,9 @@ def _bind(self, args, kwargs, *, partial=False):
30773077
break
30783078
elif param.name in kwargs:
30793079
if param.kind == _POSITIONAL_ONLY:
3080+
if param.default is _empty:
3081+
msg = f'missing a required positional-only argument: {param.name!r}'
3082+
raise TypeError(msg)
30803083
# Raise a TypeError once we are sure there is no
30813084
# **kwargs param later.
30823085
pos_only_param_in_kwargs.append(param)

Lib/test/test_inspect/test_inspect.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5149,7 +5149,11 @@ class TestSignatureBind(unittest.TestCase):
51495149
def call(func, *args, **kwargs):
51505150
sig = inspect.signature(func)
51515151
ba = sig.bind(*args, **kwargs)
5152-
return func(*ba.args, **ba.kwargs)
5152+
# Prevent unexpected success of assertRaises(TypeError, ...)
5153+
try:
5154+
return func(*ba.args, **ba.kwargs)
5155+
except TypeError as e:
5156+
raise AssertionError from e
51535157

51545158
def test_signature_bind_empty(self):
51555159
def test():
@@ -5349,7 +5353,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
53495353
self.assertEqual(self.call(test, 1, 2, c_po=4),
53505354
(1, 2, 3, 42, 50, {'c_po': 4}))
53515355

5352-
with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
5356+
with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):
53535357
self.call(test, a_po=1, b_po=2)
53545358

53555359
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)