gh-107831: Fix inspect.getcallargs accepting positional-only args by keyword by iamsharduld · Pull Request #151900 · python/cpython · GitHub
Skip to content
Open
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
16 changes: 15 additions & 1 deletion Lib/inspect.py
16 changes: 16 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,22 @@ def test_plain(self):
self.assertEqualCallArgs(f, '2, **collections.UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3)')

def test_positional_only(self):
# gh-107831: positional-only parameters cannot be filled by keyword,
# so getcallargs must match a real call (raise rather than bind).
f = self.makeCallable('a, b, /, c, d=1, *, e')
self.assertEqualCallArgs(f, '1, 2, 3, e=4')
self.assertEqualCallArgs(f, '1, 2, c=3, e=4')
self.assertEqualCallArgs(f, '1, 2, 3, 4, e=5')
self.assertEqualException(f, 'a=1, b=2, c=3, e=4')
self.assertEqualException(f, '1, b=2, c=3, e=4')
# With **kwargs the positional-only name is absorbed into it and the
# parameter itself is left unfilled (a missing-argument error).
g = self.makeCallable('a, /, **kw')
self.assertEqualCallArgs(g, '1')
self.assertEqualCallArgs(g, '1, x=2')
self.assertEqualException(g, 'a=1')

def test_varargs(self):
f = self.makeCallable('a, b=1, *c')
self.assertEqualCallArgs(f, '2')
Expand Down
Loading