Skip to content
Open
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
13 changes: 9 additions & 4 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3171,7 +3171,7 @@ def _bind(self, args, kwargs, *, partial=False):
break
elif param.name in kwargs:
if param.kind == _POSITIONAL_ONLY:
if param.default is _empty:
if not partial and 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
Expand Down Expand Up @@ -3234,26 +3234,31 @@ def _bind(self, args, kwargs, *, partial=False):
# keyword arguments
kwargs_param = None
for param in itertools.chain(parameters_ex, parameters):
if param.kind == _VAR_KEYWORD:
kind = param.kind
if kind == _VAR_KEYWORD:
# Memorize that we have a '**kwargs'-like parameter
kwargs_param = param
continue

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

param_name = param.name
if kind == _POSITIONAL_ONLY and param_name in kwargs:
pos_only_param_in_kwargs.append(param)
continue

try:
arg_val = kwargs.pop(param_name)
except KeyError:
# We have no value for this parameter. It's fine though,
# if it has a default value, or it is an '*args'-like
# parameter, left alone by the processing of positional
# arguments.
if (not partial and param.kind != _VAR_POSITIONAL and
if (not partial and kind != _VAR_POSITIONAL and
param.default is _empty):
raise TypeError('missing a required argument: {arg!r}'. \
format(arg=param_name)) from None
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6027,6 +6027,26 @@ def without_var_kwargs(c_po=3, d_po=4, /):
):
self.call(without_var_kwargs, c_po=33, d_po=44)

def test_signature_bind_omitted_positional_only(self):
def f(x=0, y=1, /, **options):
return x, y, options

bound = inspect.signature(f).bind(y=2)
bound.apply_defaults()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please, assert .args and .kwargs before .apply_defaults as well.

self.assertEqual(f(*bound.args, **bound.kwargs), f(y=2))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please, assert the result of inspect.signature(f).bind(3, 4, y=5) here as well.


def g(x=0, y=1, /):
pass

with self.assertRaises(TypeError):
inspect.signature(g).bind(y=2)

def h(x, /, **options):
pass

bound = inspect.signature(h).bind_partial(x=2)
self.assertEqual(bound.arguments, {'options': {'x': 2}})

def test_signature_bind_with_self_arg(self):
# Issue #17071: one of the parameters is named "self
def test(a, self, b):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`inspect.Signature.bind` and :meth:`~inspect.Signature.bind_partial`
when a keyword matches an omitted positional-only parameter.
Loading