Skip to content

bpo-38478: Correctly handle keyword argument with same name as positional-only parameter #16800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,7 @@ def _bind(self, args, kwargs, *, partial=False):
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
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,16 @@ def make_set():
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.