Skip to content

Commit

Permalink
Fix currying for bound functions
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 17, 2023
1 parent dabc6a7 commit 5da9c8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion param/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def _depends(*args, **kw):
for dep in deps:
if isinstance(dep, str):
string_specs = True
elif hasattr(dep, '_dinfo'):
pass
elif not isinstance(dep, Parameter):
raise ValueError('The depends decorator only accepts string '
'types referencing a parameter or parameter '
Expand Down Expand Up @@ -325,7 +327,6 @@ async def wrapped(*wargs, **wkwargs):
@depends(**dependencies, watch=watch)
def wrapped(*wargs, **wkwargs):
combined_args, combined_kwargs = combine_arguments(wargs, wkwargs)

return eval_fn()(*combined_args, **combined_kwargs)
wrapped.__bound_function__ = function
wrapped.reactive = lambda: reactive(wrapped)
Expand Down
18 changes: 18 additions & 0 deletions tests/testbind.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def test_bind_constant_args_and_kwargs():
def test_bind_constant_args_and_kwargs_partial():
assert bind(identity, 1, bar=3)(2, baz=4) == ((1, 2), {'bar': 3, 'baz': 4})

def test_curry_bind_args():
assert bind(bind(identity, 1), 2)() == ((1, 2), {})

def test_curry_bind_kwargs():
assert bind(bind(identity, foo=1), bar=2)() == ((), {'foo': 1, 'bar': 2})

def test_bind_class_param_as_arg():
bound_fn = bind(identity, Parameters.param.string)
assert bound_fn() == (('string',), {})
Expand Down Expand Up @@ -139,6 +145,18 @@ def test_bind_instance_params_and_constants_as_args_and_kwargs():
P.number = 6.28
assert bound_fn() == (('foo', 'baz',), {'num': 6.28, 'bar': 6})

def test_bind_curry_function_with_deps():
P = Parameters()
bound_fn = bind(
identity, P.param.string, num=P.param.number
)
curried_fn = bind(bound_fn, const=3)
assert curried_fn() == (('string',), {'num': 3.14, 'const': 3})
P.string = 'baz'
assert curried_fn() == (('baz',), {'num': 3.14, 'const': 3})
P.number = 6.28
assert curried_fn() == (('baz',), {'num': 6.28, 'const': 3})

def test_bind_bound_function_to_arg():
P = Parameters(integer=1)

Expand Down

0 comments on commit 5da9c8a

Please sign in to comment.