holoviz / holoviz/param

allow_refs and rx() expr not working in nested Parameterized

Open
#972 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
521
Forks
86
Avg merge
1d 13h
Merged PRs (30d)
35

Description

param 2.1.1, python 3.12.1

I am trying to assign reactive expressions to parameters defined with `allow_refs=True` in the `__init__()` of the Parameterized class, similiar to what you can achieve with param.depends(). This is not clearly documented but it seems like it should work and in fact it does for simple examples. But once you start nesting Parameterized classes it stops working.

This works:
 
```python
# %%
class SVConst(param.Parameterized):
i = param.Number(1, allow_refs=True, nested_refs=True)
f = param.Number(None, allow_refs=True, nested_refs=True)

def __init__(self, **params):
super().__init__(**params)
self.f = self.param.i.rx() / 10

sv = SVConst()

sv.i = 20
print(sv.f)

```

We get sv.f = 2.0 as expected. So far so good.

Now we add a level of hierarchy:

```python

# %%
class IPConsts(param.Parameterized):
const1 = param.ClassSelector(class_=SVConst, default=SVConst(), allow_refs=True, nested_refs=True)
const2 = param.ClassSelector(class_=SVConst, default=SVConst(), allow_refs=True, nested_refs=True)
const3 = param.ClassSelector(class_=SVConst, default=SVConst(), allow_refs=True, nested_refs=True)

def __init__(self, **params):
super().__init__(**params)
self.const3.i = self.const1.param.i.rx() + self.const2.param.i.rx()

ip = IPConsts()

ip.const1.i = 10
ip.const2.i = 40
print(ip.const3.i)
print(ip.const3.f)
```

We get ip.const3.i = 50 as expected (40 + 10).
But ip.const3.f is 0.1, but I expected it to be 5.0, i.e. 50 / 10.

Seems like a bug?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.