From f3ef06a7cb347ab7bd3cc2b0b3dcebe4f9ff36f9 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 15 Oct 2019 12:40:02 +0100 Subject: [PATCH] bpo-38478: Correctly handle keyword argument with same name as positional-only parameter (GH-16800) --- Lib/inspect.py | 2 +- Lib/test/test_inspect.py | 10 ++++++++++ .../Library/2019-10-15-11-37-57.bpo-38478.A87OPO.rst | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-10-15-11-37-57.bpo-38478.A87OPO.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index c2a1ed4148e..3ff395ca333 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2960,7 +2960,7 @@ class Signature: arguments[param.name] = tuple(values) break - if param.name in kwargs: + if param.name in kwargs and param.kind != _POSITIONAL_ONLY: raise TypeError( 'multiple values for argument {arg!r}'.format( arg=param.name)) from None diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 9d28d5ad570..d95e742c8dd 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3573,6 +3573,16 @@ class TestSignatureBind(unittest.TestCase): iterator = iter(range(5)) self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16}) + def test_signature_bind_posonly_kwargs(self): + def foo(bar, /, **kwargs): + return bar, kwargs.get(bar) + + sig = inspect.signature(foo) + result = sig.bind("pos-only", bar="keyword") + + self.assertEqual(result.kwargs, {"bar": "keyword"}) + self.assertIn(("bar", "pos-only"), result.arguments.items()) + class TestBoundArguments(unittest.TestCase): def test_signature_bound_arguments_unhashable(self): diff --git a/Misc/NEWS.d/next/Library/2019-10-15-11-37-57.bpo-38478.A87OPO.rst b/Misc/NEWS.d/next/Library/2019-10-15-11-37-57.bpo-38478.A87OPO.rst new file mode 100644 index 00000000000..b19fa23639d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-10-15-11-37-57.bpo-38478.A87OPO.rst @@ -0,0 +1,3 @@ +Fixed a bug in :meth:`inspect.signature.bind` that was causing it to fail +when handling a keyword argument with same name as positional-only parameter. +Patch by Pablo Galindo.