mirror of
https://github.com/python/cpython.git
synced 2025-09-19 23:20:25 +00:00
Issue #17071: Signature.bind() now works when one of the keyword arguments is named `self
`.
This commit is contained in:
parent
c5b75db5de
commit
bd41d1b14c
3 changed files with 17 additions and 4 deletions
|
@ -2028,19 +2028,19 @@ class Signature:
|
||||||
|
|
||||||
return self._bound_arguments_cls(self, arguments)
|
return self._bound_arguments_cls(self, arguments)
|
||||||
|
|
||||||
def bind(self, *args, **kwargs):
|
def bind(__bind_self, *args, **kwargs):
|
||||||
'''Get a BoundArguments object, that maps the passed `args`
|
'''Get a BoundArguments object, that maps the passed `args`
|
||||||
and `kwargs` to the function's signature. Raises `TypeError`
|
and `kwargs` to the function's signature. Raises `TypeError`
|
||||||
if the passed arguments can not be bound.
|
if the passed arguments can not be bound.
|
||||||
'''
|
'''
|
||||||
return self._bind(args, kwargs)
|
return __bind_self._bind(args, kwargs)
|
||||||
|
|
||||||
def bind_partial(self, *args, **kwargs):
|
def bind_partial(__bind_self, *args, **kwargs):
|
||||||
'''Get a BoundArguments object, that partially maps the
|
'''Get a BoundArguments object, that partially maps the
|
||||||
passed `args` and `kwargs` to the function's signature.
|
passed `args` and `kwargs` to the function's signature.
|
||||||
Raises `TypeError` if the passed arguments can not be bound.
|
Raises `TypeError` if the passed arguments can not be bound.
|
||||||
'''
|
'''
|
||||||
return self._bind(args, kwargs, partial=True)
|
return __bind_self._bind(args, kwargs, partial=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
result = []
|
result = []
|
||||||
|
|
|
@ -2241,6 +2241,16 @@ class TestSignatureBind(unittest.TestCase):
|
||||||
with self.assertRaisesRegex(TypeError, "parameter is positional only"):
|
with self.assertRaisesRegex(TypeError, "parameter is positional only"):
|
||||||
self.call(test, a_po=1, b_po=2)
|
self.call(test, a_po=1, b_po=2)
|
||||||
|
|
||||||
|
def test_signature_bind_with_self_arg(self):
|
||||||
|
# Issue #17071: one of the parameters is named "self
|
||||||
|
def test(a, self, b):
|
||||||
|
pass
|
||||||
|
sig = inspect.signature(test)
|
||||||
|
ba = sig.bind(1, 2, 3)
|
||||||
|
self.assertEqual(ba.args, (1, 2, 3))
|
||||||
|
ba = sig.bind(1, self=2, b=3)
|
||||||
|
self.assertEqual(ba.args, (1, 2, 3))
|
||||||
|
|
||||||
|
|
||||||
class TestBoundArguments(unittest.TestCase):
|
class TestBoundArguments(unittest.TestCase):
|
||||||
def test_signature_bound_arguments_unhashable(self):
|
def test_signature_bound_arguments_unhashable(self):
|
||||||
|
|
|
@ -164,6 +164,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #17071: Signature.bind() now works when one of the keyword arguments
|
||||||
|
is named ``self``.
|
||||||
|
|
||||||
- Issue #12004: Fix an internal error in PyZipFile when writing an invalid
|
- Issue #12004: Fix an internal error in PyZipFile when writing an invalid
|
||||||
Python file. Patch by Ben Morgan.
|
Python file. Patch by Ben Morgan.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue