Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,9 @@ def _bind(self, args, kwargs, *, partial=False):
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)
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5149,7 +5149,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():
Expand Down Expand Up @@ -5349,7 +5353,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
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'"):
Copy link
Contributor Author

@jacobtylerwalls jacobtylerwalls Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these cases could be moved to the existing method test_signature_bind_posonly_kwargs() if desired?

self.call(test, a_po=1, b_po=2)

def without_var_kwargs(c_po=3, d_po=4, /):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading