From dd20797979dfa46eb53d3a6d466d68e385f7345a Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 16 Feb 2025 08:59:01 -0500 Subject: [PATCH 1/2] gh-130164: Fix inspect.signature.bind() handling of positional-only arguments without defaults Follow-up to 9c15202. --- Lib/inspect.py | 3 +++ Lib/test/test_inspect/test_inspect.py | 8 ++++++-- .../2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index facad478103668..124293727ca84a 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -3077,6 +3077,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 8e47df21cfef2e..06785e275f6b11 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -5149,7 +5149,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(): @@ -5349,7 +5353,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..0f8358452d9b1d --- /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. From 4a0b38a44656681d261234ebe6763ade4116625e Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 16 Feb 2025 09:10:54 -0500 Subject: [PATCH 2/2] Fix typo --- .../next/Library/2025-02-16-08-56-48.gh-issue-130164.vvoaU2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0f8358452d9b1d..a4a47cb02dfcad 100644 --- 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 @@ -1,3 +1,3 @@ -Fixed failure to raise :exc:`TypeError` in :meth:`inspect.signature.bind` +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.