Skip to content

Commit

Permalink
Handle parameter references from Param 2.0 (#5906)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Sep 25, 2023
1 parent 79008ed commit 2aca2f7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
8 changes: 6 additions & 2 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class Callable(param.Parameterized):
"""

callable = param.Callable(default=None, constant=True, doc="""
The callable function being wrapped.""")
The callable function being wrapped.""", **util.disallow_refs)

inputs = param.List(default=[], constant=True, doc="""
The list of inputs the callable function is wrapping. Used
Expand Down Expand Up @@ -546,6 +546,8 @@ def __call__(self, *args, **kwargs):
# Nothing to do for callbacks that accept no arguments
kwarg_hash = kwargs.pop('_memoization_hash_', ())
(self.args, self.kwargs) = (args, kwargs)
if hasattr(self.callable, 'rx'):
return self.callable.rx.resolve()
if not args and not kwargs and not any(kwarg_hash): return self.callable()
inputs = [i for i in self.inputs if isinstance(i, DynamicMap)]
streams = []
Expand Down Expand Up @@ -772,7 +774,9 @@ def __init__(self, callback, initial_items=None, streams=None, **params):
streams = streams_list_from_dict(streams)

# If callback is a parameterized method and watch is disabled add as stream
if (params.get('watch', True) and (util.is_param_method(callback, has_deps=True) or
if util.param_version > util.Version('2.0.0rc1') and param.parameterized.resolve_ref(callback):
streams.append(callback)
elif (params.get('watch', True) and (util.is_param_method(callback, has_deps=True) or
(isinstance(callback, FunctionType) and hasattr(callback, '_dinfo')))):
streams.append(callback)

Expand Down
7 changes: 6 additions & 1 deletion holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

anonymous_dimension_label = '_'

disallow_refs = {'allow_refs': False} if param_version > Version('2.0.0rc1') else {}

# Argspec was removed in Python 3.11
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')

Expand Down Expand Up @@ -1617,7 +1619,10 @@ def resolve_dependent_value(value):
from panel.depends import param_value_if_widget
from panel.widgets import RangeSlider
range_widget = isinstance(value, RangeSlider)
value = param_value_if_widget(value)
if param_version > Version('2.0.0rc1'):
value = param.parameterized.resolve_value(value)
else:
value = param_value_if_widget(value)

if is_param_method(value, has_deps=True):
value = value()
Expand Down
20 changes: 15 additions & 5 deletions holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def streams_list_from_dict(streams):
params = {}
for k, v in streams.items():
if 'panel' in sys.modules:
from panel.depends import param_value_if_widget
v = param_value_if_widget(v)
if util.param_version > util.Version('2.0.0rc1'):
v = param.parameterized.transform_reference(v)
else:
from panel.depends import param_value_if_widget
v = param_value_if_widget(v)
if isinstance(v, param.Parameter) and v.owner is not None:
params[k] = v
else:
Expand Down Expand Up @@ -222,8 +225,15 @@ def _process_streams(cls, streams):
rename = {(p.owner, p.name): k for k, p in deps.get('kw', {}).items()}
s = Params(parameters=dep_params, rename=rename)
else:
invalid.append(s)
continue
if util.param_version > util.Version('2.0.0rc1'):
deps = param.parameterized.resolve_ref(s)
else:
deps = None
if deps:
s = Params(parameters=deps)
else:
invalid.append(s)
continue
if isinstance(s, Params):
pid = id(s.parameterized)
overlap = (set(s.parameters) & parameterizeds[pid])
Expand Down Expand Up @@ -672,7 +682,7 @@ class Params(Stream):
parameterized = param.ClassSelector(class_=(param.Parameterized,
param.parameterized.ParameterizedMetaclass),
constant=True, allow_None=True, doc="""
Parameterized instance to watch for parameter changes.""")
Parameterized instance to watch for parameter changes.""", **util.disallow_refs)

parameters = param.List(default=[], constant=True, doc="""
Parameters on the parameterized to watch.""")
Expand Down
2 changes: 1 addition & 1 deletion holoviews/tests/util/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def test_pandas_chained_methods(self):
with warnings.catch_warnings():
# The kwargs is {'axis': None} and is already handled by the code.
# This context manager can be removed, when it raises an TypeError instead of warning.
warnings.simplefilter("ignore", "Passing additional kwargs to Rolling.mean")
warnings.filterwarnings("ignore", "Passing additional kwargs to Rolling.mean")
self.assert_apply(expr, self.linear_ints.rolling(1).mean())


Expand Down
2 changes: 1 addition & 1 deletion holoviews/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ class Dynamic(param.ParameterizedFunction):
Whether the cloned DynamicMap will share the same cache.""")

streams = param.ClassSelector(default=[], class_=(list, dict), doc="""
List of streams to attach to the returned DynamicMap""")
List of streams to attach to the returned DynamicMap""", **util.disallow_refs)

def __call__(self, map_obj, **params):
watch = params.pop('watch', True)
Expand Down

0 comments on commit 2aca2f7

Please sign in to comment.