Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix watchers support when the Parameterized instance is falsey #769

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
hoxbro marked this conversation as resolved.
Show resolved Hide resolved


class TestParamDependsFunction(unittest.TestCase):

Expand Down