Skip to content

Commit 4700b7c

Browse files
maximltphilippjfr
andauthored
Fix watchers support when the Parameterized instance is falsey (#769)
Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>
1 parent 3894b4e commit 4700b7c

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

param/parameterized.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def _params_depended_on(minfo, dynamic=True, intermediate=True):
619619
deps, dynamic_deps = [], []
620620
dinfo = getattr(minfo.method, "_dinfo", {})
621621
for d in dinfo.get('dependencies', list(minfo.cls.param)):
622-
ddeps, ddynamic_deps = (minfo.inst or minfo.cls).param._spec_to_obj(d, dynamic, intermediate)
622+
ddeps, ddynamic_deps = (minfo.cls if minfo.inst is None else minfo.inst).param._spec_to_obj(d, dynamic, intermediate)
623623
dynamic_deps += ddynamic_deps
624624
for dep in ddeps:
625625
if isinstance(dep, PInfo):
@@ -1859,7 +1859,7 @@ def _update_deps(self_, attribute=None, init=False):
18591859
init_methods.append(m)
18601860
elif dynamic:
18611861
for w in obj._dynamic_watchers.pop(method, []):
1862-
(w.inst or w.cls).param.unwatch(w)
1862+
(w.cls if w.inst is None else w.inst).param.unwatch(w)
18631863
else:
18641864
continue
18651865

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

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

@@ -1930,7 +1930,7 @@ def _watch_group(self_, obj, name, queued, group, attribute=None):
19301930
on the old subobject and create watchers on the new subobject.
19311931
"""
19321932
dynamic_dep, param_dep = group[0]
1933-
dep_obj = (param_dep.inst or param_dep.cls)
1933+
dep_obj = param_dep.cls if param_dep.inst is None else param_dep.inst
19341934
params = []
19351935
for _, g in group:
19361936
if g.name not in params:

tests/testparamdepends.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,27 @@ def method1(self):
820820
assert method1_count == 0
821821
assert method2_count == 1
822822

823+
def test_param_depends_class_with_len(self):
824+
# https://github.com/holoviz/param/issues/747
825+
826+
count = 0
827+
828+
class P(param.Parameterized):
829+
x = param.Parameter()
830+
831+
@param.depends('x', watch=True)
832+
def debug(self):
833+
nonlocal count
834+
count += 1
835+
836+
# bool(P()) evaluates to False
837+
def __len__(self):
838+
return 0
839+
840+
p = P()
841+
p.x = 1
842+
assert count == 1
843+
823844

824845
class TestParamDependsFunction(unittest.TestCase):
825846

0 commit comments

Comments
 (0)