Skip to content

Commit ef61ef6

Browse files
authored
gh-162668: preserve keywords matching omitted positional-only parameters in inspect (#162669)
* gh-162668: preserve keywords matching omitted positional-only parameters in inspect * gh-162668: check positional only binding before applying defaults
1 parent 0946b2a commit ef61ef6

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎Lib/inspect.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ def _bind(self, args, kwargs, *, partial=False):
31713171
break
31723172
elif param.name in kwargs:
31733173
if param.kind == _POSITIONAL_ONLY:
3174-
if param.default is _empty:
3174+
if not partial and param.default is _empty:
31753175
msg = f'missing a required positional-only argument: {param.name!r}'
31763176
raise TypeError(msg)
31773177
# Raise a TypeError once we are sure there is no
@@ -3234,26 +3234,31 @@ def _bind(self, args, kwargs, *, partial=False):
32343234
# keyword arguments
32353235
kwargs_param = None
32363236
for param in itertools.chain(parameters_ex, parameters):
3237-
if param.kind == _VAR_KEYWORD:
3237+
kind = param.kind
3238+
if kind == _VAR_KEYWORD:
32383239
# Memorize that we have a '**kwargs'-like parameter
32393240
kwargs_param = param
32403241
continue
32413242

3242-
if param.kind == _VAR_POSITIONAL:
3243+
if kind == _VAR_POSITIONAL:
32433244
# Named arguments don't refer to '*args'-like parameters.
32443245
# We only arrive here if the positional arguments ended
32453246
# before reaching the last parameter before *args.
32463247
continue
32473248

32483249
param_name = param.name
3250+
if kind == _POSITIONAL_ONLY and param_name in kwargs:
3251+
pos_only_param_in_kwargs.append(param)
3252+
continue
3253+
32493254
try:
32503255
arg_val = kwargs.pop(param_name)
32513256
except KeyError:
32523257
# We have no value for this parameter. It's fine though,
32533258
# if it has a default value, or it is an '*args'-like
32543259
# parameter, left alone by the processing of positional
32553260
# arguments.
3256-
if (not partial and param.kind != _VAR_POSITIONAL and
3261+
if (not partial and kind != _VAR_POSITIONAL and
32573262
param.default is _empty):
32583263
raise TypeError('missing a required argument: {arg!r}'. \
32593264
format(arg=param_name)) from None

‎Lib/test/test_inspect/test_inspect.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6027,6 +6027,32 @@ def without_var_kwargs(c_po=3, d_po=4, /):
60276027
):
60286028
self.call(without_var_kwargs, c_po=33, d_po=44)
60296029

6030+
def test_signature_bind_omitted_positional_only(self):
6031+
def f(x=0, y=1, /, **options):
6032+
return x, y, options
6033+
6034+
bound = inspect.signature(f).bind(y=2)
6035+
self.assertEqual(bound.args, ())
6036+
self.assertEqual(bound.kwargs, {'y': 2})
6037+
bound.apply_defaults()
6038+
self.assertEqual(f(*bound.args, **bound.kwargs), f(y=2))
6039+
6040+
bound = inspect.signature(f).bind(3, 4, y=5)
6041+
self.assertEqual(bound.args, (3, 4))
6042+
self.assertEqual(bound.kwargs, {'y': 5})
6043+
6044+
def g(x=0, y=1, /):
6045+
pass
6046+
6047+
with self.assertRaises(TypeError):
6048+
inspect.signature(g).bind(y=2)
6049+
6050+
def h(x, /, **options):
6051+
pass
6052+
6053+
bound = inspect.signature(h).bind_partial(x=2)
6054+
self.assertEqual(bound.arguments, {'options': {'x': 2}})
6055+
60306056
def test_signature_bind_with_self_arg(self):
60316057
# Issue #17071: one of the parameters is named "self
60326058
def test(a, self, b):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`inspect.Signature.bind` and :meth:`~inspect.Signature.bind_partial`
2+
when a keyword matches an omitted positional-only parameter.

0 commit comments

Comments
 (0)