fix var dependency over properties (#3588)

This commit is contained in:
benedikt-bartscher 2024-06-28 21:48:28 +02:00 committed by GitHub
parent 33d7ec1f04
commit a6bdaf1bbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 3 deletions

View File

@ -2174,8 +2174,21 @@ class ComputedVar(Var, property):
obj=ref_obj,
)
)
else:
# normal attribute access
# recurse into property fget functions
elif isinstance(ref_obj, property) and not isinstance(
ref_obj, ComputedVar
):
d.update(
self._deps(
objclass=objclass,
obj=ref_obj.fget, # type: ignore
)
)
elif (
instruction.argval in objclass.backend_vars
or instruction.argval in objclass.vars
):
# var access
d.add(instruction.argval)
elif instruction.opname == "LOAD_CONST" and isinstance(
instruction.argval, CodeType

View File

@ -1278,6 +1278,10 @@ def test_computed_var_dependencies():
y: List[int] = [1, 2, 3]
_z: List[int] = [1, 2, 3]
@property
def testprop(self) -> int:
return self.v
@rx.var(cache=True)
def comp_v(self) -> int:
"""Direct access.
@ -1287,6 +1291,15 @@ def test_computed_var_dependencies():
"""
return self.v
@rx.var(cache=True)
def comp_v_via_property(self) -> int:
"""Access v via property.
Returns:
The value of v via property.
"""
return self.testprop
@rx.var(cache=True)
def comp_w(self):
"""Nested lambda.
@ -1328,7 +1341,7 @@ def test_computed_var_dependencies():
return [z in self._z for z in range(5)]
cs = ComputedState()
assert cs._computed_var_dependencies["v"] == {"comp_v"}
assert cs._computed_var_dependencies["v"] == {"comp_v", "comp_v_via_property"}
assert cs._computed_var_dependencies["w"] == {"comp_w"}
assert cs._computed_var_dependencies["x"] == {"comp_x"}
assert cs._computed_var_dependencies["y"] == {"comp_y"}