-
-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically handle different signatures in Callable #1260
Merged
Merged
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
80176f1
Added core.util.argspec utility
jlstevens bbbcf80
Added Callable.argspec property
jlstevens ce4b284
Callable now promotes posargs to kwargs if possible
jlstevens edb2606
Added assertContains method to MockLoggingHandler
jlstevens 2be3cbb
Added 18 Callable unit tests in tests/testcallable.py
jlstevens 51a1016
Fixed typo in docstring of argspec utility
jlstevens a60ec5c
Fixed py3 handling of ParameterizedFunction.__call__ in argspec
jlstevens c05d369
Added Callable tests of parameterized function instances
jlstevens c74f3ea
Added validate_dynamic_argspec utility to core.util
jlstevens 3f3655d
Validated callback signature in DynamicMap constructor
jlstevens 25c91b6
Generalized how DynamicMap._execute_callback invokes Callable
jlstevens 50022df
Computing *args and **kwargs outside of memoization context manager
jlstevens 3d5be47
Fixed invalid unit test definitions in testdynamic.py
jlstevens d5f060e
validate_dynamic_argspec now handles positional stream arguments
jlstevens 32b3811
validate_dynamic_argspec now supports streams as positional args
jlstevens d5063f9
Fixed bug in validate_dynamic_argspec utility
jlstevens f5efe4e
validate_dynamic_argspec now only raises KeyError exceptions
jlstevens 0fae1e5
Added 10 unit tests for different allowed Callable signatures
jlstevens 09fec6d
Added unit test for when kdim posargs have different names
jlstevens ebc8ffa
Improved validation in validate_dynamic_argspec utility
jlstevens fccdab4
Formatting fixes to comments
jlstevens 5ec7a2b
Added unit tests for invalid call conditions
jlstevens 111e333
Unmatched positional kdims must be at the start of the signature
jlstevens f5c370c
Fixed exception message
jlstevens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,119 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Unit tests of the Callable object that wraps user callbacks | ||
""" | ||
import param | ||
from holoviews.element.comparison import ComparisonTestCase | ||
from holoviews.core.spaces import Callable | ||
from functools import partial | ||
|
||
from . import LoggingComparisonTestCase | ||
|
||
class CallableClass(object): | ||
|
||
def __call__(self, *testargs): | ||
return sum(testargs) | ||
|
||
|
||
class ParamFunc(param.ParameterizedFunction): | ||
|
||
a = param.Integer(default=1) | ||
b = param.Number(default=1) | ||
|
||
def __call__(self, **params): | ||
p = param.ParamOverrides(self, params) | ||
return p.a * p.b | ||
|
||
|
||
class TestSimpleCallableInvocation(LoggingComparisonTestCase): | ||
|
||
def setUp(self): | ||
super(TestSimpleCallableInvocation, self).setUp() | ||
|
||
def test_callable_fn(self): | ||
def callback(x): return x | ||
self.assertEqual(Callable(callback)(3), 3) | ||
|
||
def test_callable_lambda(self): | ||
self.assertEqual(Callable(lambda x,y: x+y)(3,5), 8) | ||
|
||
def test_callable_lambda_extras(self): | ||
substr = "Ignoring extra positional argument" | ||
self.assertEqual(Callable(lambda x,y: x+y)(3,5,10), 8) | ||
self.log_handler.assertContains('WARNING', substr) | ||
|
||
def test_callable_lambda_extras_kwargs(self): | ||
substr = "['x'] overriden by keywords" | ||
self.assertEqual(Callable(lambda x,y: x+y)(3,5,x=10), 15) | ||
self.log_handler.assertEndsWith('WARNING', substr) | ||
|
||
def test_callable_partial(self): | ||
self.assertEqual(Callable(partial(lambda x,y: x+y,x=4))(5), 9) | ||
|
||
def test_callable_class(self): | ||
self.assertEqual(Callable(CallableClass())(1,2,3,4), 10) | ||
|
||
def test_callable_paramfunc(self): | ||
self.assertEqual(Callable(ParamFunc)(a=3,b=5), 15) | ||
|
||
|
||
class TestCallableArgspec(ComparisonTestCase): | ||
|
||
def test_callable_fn_argspec(self): | ||
def callback(x): return x | ||
self.assertEqual(Callable(callback).argspec.args, ['x']) | ||
self.assertEqual(Callable(callback).argspec.keywords, None) | ||
|
||
def test_callable_lambda_argspec(self): | ||
self.assertEqual(Callable(lambda x,y: x+y).argspec.args, ['x','y']) | ||
self.assertEqual(Callable(lambda x,y: x+y).argspec.keywords, None) | ||
|
||
def test_callable_partial_argspec(self): | ||
self.assertEqual(Callable(partial(lambda x,y: x+y,x=4)).argspec.args, ['y']) | ||
self.assertEqual(Callable(partial(lambda x,y: x+y,x=4)).argspec.keywords, None) | ||
|
||
def test_callable_class_argspec(self): | ||
self.assertEqual(Callable(CallableClass()).argspec.args, []) | ||
self.assertEqual(Callable(CallableClass()).argspec.keywords, None) | ||
self.assertEqual(Callable(CallableClass()).argspec.varargs, 'testargs') | ||
|
||
def test_callable_paramfunc_argspec(self): | ||
self.assertEqual(Callable(ParamFunc).argspec.args, []) | ||
self.assertEqual(Callable(ParamFunc).argspec.keywords, 'params') | ||
self.assertEqual(Callable(ParamFunc).argspec.varargs, None) | ||
|
||
|
||
class TestKwargCallableInvocation(ComparisonTestCase): | ||
""" | ||
Test invocation of Callable with kwargs, even for callbacks with | ||
positional arguments. | ||
""" | ||
|
||
def test_callable_fn(self): | ||
def callback(x): return x | ||
self.assertEqual(Callable(callback)(x=3), 3) | ||
|
||
def test_callable_lambda(self): | ||
self.assertEqual(Callable(lambda x,y: x+y)(x=3,y=5), 8) | ||
|
||
def test_callable_partial(self): | ||
self.assertEqual(Callable(partial(lambda x,y: x+y,x=4))(y=5), 9) | ||
|
||
def test_callable_paramfunc(self): | ||
self.assertEqual(Callable(ParamFunc)(a=3,b=5), 15) | ||
|
||
|
||
class TestMixedCallableInvocation(ComparisonTestCase): | ||
""" | ||
Test mixed invocation of Callable with kwargs. | ||
""" | ||
|
||
def test_callable_mixed_1(self): | ||
def mixed_example(a,b, c=10, d=20): | ||
return a+b+c+d | ||
self.assertEqual(Callable(mixed_example)(a=3,b=5), 38) | ||
|
||
def test_callable_mixed_2(self): | ||
def mixed_example(a,b, c=10, d=20): | ||
return a+b+c+d | ||
self.assertEqual(Callable(mixed_example)(3,5,5), 33) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/with/which?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Fixed in 51a1016