Skip to content

Commit e70edd7

Browse files
pablogsalJake Taylor
authored and
Jake Taylor
committed
bpo-38478: Correctly handle keyword argument with same name as positional-only parameter (pythonGH-16800)
1 parent 14309d4 commit e70edd7

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
@@ -3573,6 +3573,16 @@ def make_set():
35733573
iterator = iter(range(5))
35743574
self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
35753575

3576+
def test_signature_bind_posonly_kwargs(self):
3577+
def foo(bar, /, **kwargs):
3578+
return bar, kwargs.get(bar)
3579+
3580+
sig = inspect.signature(foo)
3581+
result = sig.bind("pos-only", bar="keyword")
3582+
3583+
self.assertEqual(result.kwargs, {"bar": "keyword"})
3584+
self.assertIn(("bar", "pos-only"), result.arguments.items())
3585+
35763586

35773587
class TestBoundArguments(unittest.TestCase):
35783588
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)