diff --git a/Lib/inspect.py b/Lib/inspect.py index 81ea5372eec235f..024717dfd10509b 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 @@ -3234,18 +3234,23 @@ 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: @@ -3253,7 +3258,7 @@ def _bind(self, args, kwargs, *, partial=False): # 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 diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index ef9d2061611f335..194465cd0f3b170 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -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() + self.assertEqual(f(*bound.args, **bound.kwargs), f(y=2)) + + 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): diff --git a/Misc/NEWS.d/next/Library/2026-11-25-20-25-00.gh-issue-162668.K6rZ1c.rst b/Misc/NEWS.d/next/Library/2026-11-25-20-25-00.gh-issue-162668.K6rZ1c.rst new file mode 100644 index 000000000000000..8077b960d424dc2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-25-20-25-00.gh-issue-162668.K6rZ1c.rst @@ -0,0 +1,2 @@ +Fix :meth:`inspect.Signature.bind` and :meth:`~inspect.Signature.bind_partial` +when a keyword matches an omitted positional-only parameter.