FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults by jacobtylerwalls · Pull Request #130192 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Lib/inspect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,9 @@ def _bind(self, args, kwargs, *, partial=False):
break
elif param.name in kwargs:
if param.kind == _POSITIONAL_ONLY:
if param.default is _empty:
msg = f'missing a required positional-only argument: {param.name!r}'
raise TypeError(msg)
# Raise a TypeError once we are sure there is no
# **kwargs param later.
pos_only_param_in_kwargs.append(param)
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_inspect/test_inspect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5149,7 +5149,11 @@ class TestSignatureBind(unittest.TestCase):
def call(func, *args, **kwargs):
sig = inspect.signature(func)
ba = sig.bind(*args, **kwargs)
return func(*ba.args, **ba.kwargs)
# Prevent unexpected success of assertRaises(TypeError, ...)
try:
return func(*ba.args, **ba.kwargs)
except TypeError as e:
raise AssertionError from e

def test_signature_bind_empty(self):
def test():
Expand Down Expand Up @@ -5349,7 +5353,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
self.assertEqual(self.call(test, 1, 2, c_po=4),
(1, 2, 3, 42, 50, {'c_po': 4}))

with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):

jacobtylerwalls Feb 16, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some of these cases could be moved to the existing method test_signature_bind_posonly_kwargs() if desired?

self.call(test, a_po=1, b_po=2)

def without_var_kwargs(c_po=3, d_po=4, /):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed failure to raise :exc:`TypeError` in :meth:`inspect.Signature.bind`
for positional-only arguments provided by keyword when a variadic keyword
argument (e.g. ``**kwargs``) is present.

Back | FazBrowse Home | New Git URL