Closes #14: Fix binding with self as a kwarg.

This commit is contained in:
Robert Collins 2015-07-09 20:29:25 +12:00
parent db7f0afe3e
commit 32a9d3e37b
3 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,11 @@
Changelog
---------
0.5
```
* Fix binding with self as a kwarg. (Robert Collins #14)
0.4 (2013-12-20)
````````````````
* Fix unbound methods getting their first parameter curried

View File

@ -769,16 +769,16 @@ class Signature(object):
# Process our '**kwargs'-like parameter
arguments[kwargs_param.name] = kwargs
else:
raise TypeError('too many keyword arguments')
raise TypeError('too many keyword arguments %r' % kwargs)
return self._bound_arguments_cls(self, arguments)
def bind(self, *args, **kwargs):
def bind(*args, **kwargs):
'''Get a BoundArguments object, that maps the passed `args`
and `kwargs` to the function's signature. Raises `TypeError`
if the passed arguments can not be bound.
'''
return self._bind(args, kwargs)
return args[0]._bind(args[1:], kwargs)
def bind_partial(self, *args, **kwargs):
'''Get a BoundArguments object, that partially maps the

View File

@ -1,6 +1,7 @@
# Copyright 2001-2013 Python Software Foundation; All Rights Reserved
from __future__ import absolute_import, division, print_function
import collections
import functools
import sys
try:
@ -982,6 +983,15 @@ def test_signature_bind_positional_only(self):
self.call(test, a_po=1, b_po=2)
""")
def test_bind_self(self):
class F:
def f(a, self):
return a, self
an_f = F()
partial_f = functools.partial(F.f, an_f)
ba = inspect.signature(partial_f).bind(self=10)
self.assertEqual((an_f, 10), partial_f(*ba.args, **ba.kwargs))
class TestBoundArguments(unittest.TestCase):