Skip to content

Commit aa4a1aa

Browse files
committed
gh-162668: preserve keywords matching omitted positional-only parameters in inspect
1 parent b0b0005 commit aa4a1aa

3 files changed

Lines changed: 31 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6027,6 +6027,26 @@ 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+
bound.apply_defaults()
6036+
self.assertEqual(f(*bound.args, **bound.kwargs), f(y=2))
6037+
6038+
def g(x=0, y=1, /):
6039+
pass
6040+
6041+
with self.assertRaises(TypeError):
6042+
inspect.signature(g).bind(y=2)
6043+
6044+
def h(x, /, **options):
6045+
pass
6046+
6047+
bound = inspect.signature(h).bind_partial(x=2)
6048+
self.assertEqual(bound.arguments, {'options': {'x': 2}})
6049+
60306050
def test_signature_bind_with_self_arg(self):
60316051
# Issue #17071: one of the parameters is named "self
60326052
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)