8000 bpo-38478: Correctly handle keyword argument with same name as positi… · python/cpython@f705f8e · GitHub
[go: up one dir, main page]

Skip to content

Commit f705f8e

Browse files
bpo-38478: Correctly handle keyword argument with same name as positional-only parameter (GH-16800)
(cherry picked from commit f3ef06a) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
1 parent f82ce5b commit f705f8e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ def _bind(self, args, kwargs, *, partial=False):
29602960
arguments[param.name] = tuple(values)
29612961
break
29622962

2963-
if param.name in kwargs:
2963+
if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
29642964
raise TypeError(
29652965
'multiple values for argument {arg!r}'.format(
29662966
arg=param.name)) from None

Lib/test/test_inspect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,16 @@ def make_set():
35773577
iterator = iter(range(5))
35783578
self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
35793579

3580+
def test_signature_bind_posonly_kwargs(self):
3581+
def foo(bar, /, **kwargs):
3582+
return bar, kwargs.get(bar)
3583+
3584+
sig = inspect.signature(foo)
3585+
result = sig.bind("pos-only", bar="keyword")
3586+
3587+
self.assertEqual(result.kwargs, {"bar": "keyword"})
3588+
self.assertIn(("bar", "pos-only"), result.arguments.items())
3589+
35803590

35813591
class TestBoundArguments(unittest.TestCase):
35823592
def test_signature_bound_arguments_unhashable(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a bug in :meth:`inspect.signature.bind` that was causing it to fail
2+
when handling a keyword argument with same name as positional-only parameter.
3+
Patch by Pablo Galindo.

0 commit comments

Comments
 (0)
0