Skip to content

Commit

Permalink
Allow depending on and binding generator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 9, 2024
1 parent a26fe66 commit 0d28576
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
15 changes: 13 additions & 2 deletions param/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def depends(func, *dependencies, watch=False, on_init=False, **kw):
{key: transform_reference(arg) for key, arg in kw.items()}
)

if inspect.isasyncgenfunction(func):
if inspect.isgeneratorfunction(func):
@wraps(func)
def _depends(*args, **kw):
for val in func(*args, **kw):
yield val
elif inspect.isasyncgenfunction(func):
@wraps(func)
async def _depends(*args, **kw):
async for val in func(*args, **kw):
Expand Down Expand Up @@ -79,7 +84,13 @@ def _depends(*args, **kw):
'parameters by name.')

if not string_specs and watch: # string_specs case handled elsewhere (later), in Parameterized.__init__
if inspect.isasyncgenfunction(func):
if inspect.isgeneratorfunction(func):
def cb(*events):
args = (getattr(dep.owner, dep.name) for dep in dependencies)
dep_kwargs = {n: getattr(dep.owner, dep.name) for n, dep in kw.items()}
for val in func(*args, **dep_kwargs):
yield val
elif inspect.isasyncgenfunction(func):
async def cb(*events):
args = (getattr(dep.owner, dep.name) for dep in dependencies)
dep_kwargs = {n: getattr(dep.owner, dep.name) for n, dep in kw.items()}
Expand Down
12 changes: 11 additions & 1 deletion param/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,17 @@ def eval_fn():
fn = eval_function_with_deps(p)
return fn

if inspect.isasyncgenfunction(function):
if inspect.isgeneratorfunction(function):
def wrapped(*wargs, **wkwargs):
combined_args, combined_kwargs = combine_arguments(
wargs, wkwargs, asynchronous=True
)
evaled = eval_fn()(*combined_args, **combined_kwargs)
for val in evaled:
yield val
wrapper_fn = depends(**dependencies, watch=watch)(wrapped)
wrapped._dinfo = wrapper_fn._dinfo
elif inspect.isasyncgenfunction(function):
async def wrapped(*wargs, **wkwargs):
combined_args, combined_kwargs = combine_arguments(
wargs, wkwargs, asynchronous=True
Expand Down

0 comments on commit 0d28576

Please sign in to comment.