Skip to content

Commit

Permalink
Ensure partial methods can be used as dmap callables (#6063)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoMathurin committed Jan 23, 2024
1 parent d4491bd commit 6dc23ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ def argspec(callable_obj):
arglen = len(callable_obj.args)
spec = inspect.getfullargspec(callable_obj.func)
args = [arg for arg in spec.args[arglen:] if arg not in callable_obj.keywords]
if inspect.ismethod(callable_obj.func):
args = args[1:]
elif inspect.ismethod(callable_obj): # instance and class methods
spec = inspect.getfullargspec(callable_obj)
args = spec.args[1:]
Expand Down
26 changes: 26 additions & 0 deletions holoviews/tests/core/test_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def somestaticmethod(): pass
@classmethod
def someclsmethod(cls): pass

def someinstancemethod(self, x, y):
return x + y

def __call__(self, *testargs):
return sum(testargs)

Expand Down Expand Up @@ -66,6 +69,9 @@ def test_callable_class_name(self):
def test_callable_class_call_method(self):
self.assertEqual(Callable(CallableClass().__call__).name, 'CallableClass')

def test_callable_instance_method(self):
assert Callable(CallableClass().someinstancemethod).name == 'CallableClass.someinstancemethod'

def test_classmethod_name(self):
self.assertEqual(Callable(CallableClass().someclsmethod).name,
'CallableClass.someclsmethod')
Expand Down Expand Up @@ -116,6 +122,12 @@ def test_callable_partial(self):
def test_callable_class(self):
self.assertEqual(Callable(CallableClass())(1,2,3,4), 10)

def test_callable_instance_method(self):
assert Callable(CallableClass().someinstancemethod)(1, 2) == 3

def test_callable_partial_instance_method(self):
assert Callable(partial(CallableClass().someinstancemethod, x=1))(2) == 3

def test_callable_paramfunc(self):
self.assertEqual(Callable(ParamFunc)(3,b=5), 15)

Expand Down Expand Up @@ -143,6 +155,14 @@ def test_callable_class_argspec(self):
self.assertEqual(Callable(CallableClass()).argspec.keywords, None)
self.assertEqual(Callable(CallableClass()).argspec.varargs, 'testargs')

def test_callable_instance_method(self):
assert Callable(CallableClass().someinstancemethod).argspec.args == ['x', 'y']
assert Callable(CallableClass().someinstancemethod).argspec.keywords is None

def test_callable_partial_instance_method(self):
assert Callable(partial(CallableClass().someinstancemethod, x=1)).argspec.args == ['y']
assert Callable(partial(CallableClass().someinstancemethod, x=1)).argspec.keywords is None

def test_callable_paramfunc_argspec(self):
self.assertEqual(Callable(ParamFunc).argspec.args, ['a'])
self.assertEqual(Callable(ParamFunc).argspec.keywords, 'params')
Expand Down Expand Up @@ -170,6 +190,12 @@ def test_callable_lambda(self):
def test_callable_partial(self):
self.assertEqual(Callable(partial(lambda x,y: x+y,x=4))(y=5), 9)

def test_callable_instance_method(self):
assert Callable(CallableClass().someinstancemethod)(x=1, y=2) == 3

def test_callable_partial_instance_method(self):
assert Callable(partial(CallableClass().someinstancemethod, x=1))(y=2) == 3

def test_callable_paramfunc(self):
self.assertEqual(Callable(ParamFunc)(a=3,b=5), 15)

Expand Down

0 comments on commit 6dc23ae

Please sign in to comment.