Skip to content

Commit

Permalink
Ensure refs can be updated by watcher of the same parameter (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed May 2, 2024
1 parent dae8fc4 commit 3e1439d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,12 +1471,13 @@ def __set__(self, obj, val):
item in a list).
"""
name = self.name
if obj is not None and self.allow_refs and obj._param__private.initialized and name not in obj._param__private.syncing:
if obj is not None and self.allow_refs and obj._param__private.initialized:
syncing = name in obj._param__private.syncing
ref, deps, val, is_async = obj.param._resolve_ref(self, val)
refs = obj._param__private.refs
if ref is not None:
self.owner.param._update_ref(name, ref)
elif name in refs:
elif name in refs and not syncing:
del refs[name]
if name in obj._param__private.async_refs:
obj._param__private.async_refs.pop(name).cancel()
Expand Down
32 changes: 32 additions & 0 deletions tests/testreactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,35 @@ def debug(value, info):
arx.rx.value = 'd'
assert expr.rx.value == 'DC'
assert computed == ['1', '2', '2', '1']


def test_ensure_ref_can_update_by_watcher_of_same_parameter():
# https://github.com/holoviz/param/pull/929

class W(param.Parameterized):
value = param.String()


class T(param.Parameterized):
lst = param.List(allow_refs=True, allow_None=True)

@param.depends("lst", watch=True)
def test(self):
lst = self.lst or range(5)
items = [W(value=str(i)) for i in lst]
with param.discard_events(self):
self.lst = param.rx(items).rx.resolve()
self.items = items

def transform(obj):
if isinstance(obj, W):
return obj.param.value
return obj


param.reactive.register_reference_transform(transform)

t = T()
t.lst = list("ABCDE")
t.items[1].value = "TEST"
assert t.lst[1] == "TEST"

0 comments on commit 3e1439d

Please sign in to comment.