mirror of
https://github.com/python/cpython.git
synced 2025-12-05 00:52:25 +00:00
gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults (GH-130192)
Follow-up to 9c15202.
This commit is contained in:
parent
01ba7df499
commit
dab456dcef
3 changed files with 12 additions and 2 deletions
|
|
@ -3077,6 +3077,9 @@ class Signature:
|
||||||
break
|
break
|
||||||
elif param.name in kwargs:
|
elif param.name in kwargs:
|
||||||
if param.kind == _POSITIONAL_ONLY:
|
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
|
# Raise a TypeError once we are sure there is no
|
||||||
# **kwargs param later.
|
# **kwargs param later.
|
||||||
pos_only_param_in_kwargs.append(param)
|
pos_only_param_in_kwargs.append(param)
|
||||||
|
|
|
||||||
|
|
@ -5149,7 +5149,11 @@ class TestSignatureBind(unittest.TestCase):
|
||||||
def call(func, *args, **kwargs):
|
def call(func, *args, **kwargs):
|
||||||
sig = inspect.signature(func)
|
sig = inspect.signature(func)
|
||||||
ba = sig.bind(*args, **kwargs)
|
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_signature_bind_empty(self):
|
||||||
def test():
|
def test():
|
||||||
|
|
@ -5349,7 +5353,7 @@ class TestSignatureBind(unittest.TestCase):
|
||||||
self.assertEqual(self.call(test, 1, 2, c_po=4),
|
self.assertEqual(self.call(test, 1, 2, c_po=4),
|
||||||
(1, 2, 3, 42, 50, {'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)
|
self.call(test, a_po=1, b_po=2)
|
||||||
|
|
||||||
def without_var_kwargs(c_po=3, d_po=4, /):
|
def without_var_kwargs(c_po=3, d_po=4, /):
|
||||||
|
|
|
||||||
|
|
@ -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…
Add table
Add a link
Reference in a new issue