https://github.com/python/cpython/commit/5c658c11f7232124704c1ee2b2efce320d68f956 commit: 5c658c11f7232124704c1ee2b2efce320d68f956 branch: 3.13 author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com> committer: serhiy-storchaka <storch...@gmail.com> date: 2025-04-08T09:08:06Z summary:
[3.13] gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults (GH-130192) (GH-130271) Follow-up to 9c15202. (cherry picked from commit dab456dcefd886bde44eb204dc6f1b2f14de0e9d) Co-authored-by: Jacob Walls <jacobtylerwa...@gmail.com> files: A Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst M Lib/inspect.py M Lib/test/test_inspect/test_inspect.py diff --git a/Lib/inspect.py b/Lib/inspect.py index 81be29a33b577d..401a76391aa1e2 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -3150,6 +3150,9 @@ def _bind(self, args, kwargs, *, partial=False): break elif param.name in kwargs: 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 # **kwargs param later. pos_only_param_in_kwargs.append(param) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index a88f1713210e1b..5e46f4fe61b559 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -5300,7 +5300,11 @@ class TestSignatureBind(unittest.TestCase): def call(func, *args, **kwargs): sig = inspect.signature(func) 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(): @@ -5500,7 +5504,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs): self.assertEqual(self.call(test, 1, 2, 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) def without_var_kwargs(c_po=3, d_po=4, /): diff --git a/Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst b/Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst new file mode 100644 index 00000000000000..a4a47cb02dfcad --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst @@ -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. _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com