Skip to content

Commit

Permalink
Fix watchers support when the Parameterized instance is falsey (#769)
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Rudiger <[email protected]>
  • Loading branch information
maximlt and philippjfr authored Jun 22, 2023
1 parent 3894b4e commit 4700b7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def _params_depended_on(minfo, dynamic=True, intermediate=True):
deps, dynamic_deps = [], []
dinfo = getattr(minfo.method, "_dinfo", {})
for d in dinfo.get('dependencies', list(minfo.cls.param)):
ddeps, ddynamic_deps = (minfo.inst or minfo.cls).param._spec_to_obj(d, dynamic, intermediate)
ddeps, ddynamic_deps = (minfo.cls if minfo.inst is None else minfo.inst).param._spec_to_obj(d, dynamic, intermediate)
dynamic_deps += ddynamic_deps
for dep in ddeps:
if isinstance(dep, PInfo):
Expand Down Expand Up @@ -1859,7 +1859,7 @@ def _update_deps(self_, attribute=None, init=False):
init_methods.append(m)
elif dynamic:
for w in obj._dynamic_watchers.pop(method, []):
(w.inst or w.cls).param.unwatch(w)
(w.cls if w.inst is None else w.inst).param.unwatch(w)
else:
continue

Expand Down Expand Up @@ -1894,7 +1894,7 @@ def _resolve_dynamic_deps(self, obj, dynamic_dep, param_dep, attribute):
subobj = getattr(subobj, subpath.split(':')[0], None)
subobjs.append(subobj)

dep_obj = (param_dep.inst or param_dep.cls)
dep_obj = param_dep.cls if param_dep.inst is None else param_dep.inst
if dep_obj not in subobjs[:-1]:
return None, None, param_dep.what

Expand Down Expand Up @@ -1930,7 +1930,7 @@ def _watch_group(self_, obj, name, queued, group, attribute=None):
on the old subobject and create watchers on the new subobject.
"""
dynamic_dep, param_dep = group[0]
dep_obj = (param_dep.inst or param_dep.cls)
dep_obj = param_dep.cls if param_dep.inst is None else param_dep.inst
params = []
for _, g in group:
if g.name not in params:
Expand Down
21 changes: 21 additions & 0 deletions tests/testparamdepends.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,27 @@ def method1(self):
assert method1_count == 0
assert method2_count == 1

def test_param_depends_class_with_len(self):
# https://github.com/holoviz/param/issues/747

count = 0

class P(param.Parameterized):
x = param.Parameter()

@param.depends('x', watch=True)
def debug(self):
nonlocal count
count += 1

# bool(P()) evaluates to False
def __len__(self):
return 0

p = P()
p.x = 1
assert count == 1


class TestParamDependsFunction(unittest.TestCase):

Expand Down

0 comments on commit 4700b7c

Please sign in to comment.