Skip to content

Commit

Permalink
Resolve non watched references too to catch bad references (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Jul 11, 2023
1 parent 42ec03e commit 6692574
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
5 changes: 2 additions & 3 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -3033,12 +3033,11 @@ def __init__(mcs, name, bases, dict_):
for name, method, dinfo in dependers:
watch = dinfo.get('watch', False)
on_init = dinfo.get('on_init', False)
if not watch:
continue
minfo = MInfo(cls=mcs, inst=None, name=name,
method=method)
deps, dynamic_deps = _params_depended_on(minfo, dynamic=False)
_watch.append((name, watch == 'queued', on_init, deps, dynamic_deps))
if watch:
_watch.append((name, watch == 'queued', on_init, deps, dynamic_deps))

# Resolve dependencies in class hierarchy
_inherited = []
Expand Down
50 changes: 41 additions & 9 deletions tests/testparamdepends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,32 @@ def __len__(self):
p.x = 1
assert count == 1

def test_param_depends_subobject_before_super_init(self):
count = 0

class X(param.Parameterized):
p = param.Parameter()

class Y(param.Parameterized):

def __init__(self, **params):
self.x = X()
super().__init__(**params)

# Check that creating this class doesn't error when resolving x.p
@param.depends('x.p')
def cb(self):
nonlocal count
count += 1

y = Y()
pinfos = y.param.method_dependencies('cb')
assert len(pinfos) == 1
pinfo = pinfos[0]
assert pinfo.inst == y.x
assert pinfo.cls == X
assert pinfo.name == 'p'


class TestParamDependsFunction:

Expand Down Expand Up @@ -1247,17 +1273,23 @@ async def function(value):


def test_misspelled_parameter_in_depends():
class Example(param.Parameterized):
xlim = param.Range((0, 10), bounds=(0, 100))
with pytest.raises(AttributeError, match="Attribute 'tlim' could not be resolved on"):
class Example(param.Parameterized):
xlim = param.Range((0, 10), bounds=(0, 100))

@param.depends("tlim") # <- Misspelled xlim
def test(self):
return True

@param.depends("tlim") # <- Misspelled xlim
def test(self):
return True

example = Example()
with pytest.raises(AttributeError, match="Attribute 'tlim' could not be resolved on"):
# Simulating: pn.panel(example.test)
example.param.method_dependencies(example.test.__name__)
def test_misspelled_parameter_in_depends_non_Parameter():
with pytest.raises(AttributeError, match="Attribute 'foo' could not be resolved on"):
class Example(param.Parameterized):
foo = 1

@param.depends("foo") # <- Not a Parameter
def test(self):
return True


def test_misspelled_parameter_in_depends_watch():
Expand Down

0 comments on commit 6692574

Please sign in to comment.