Selaa lähdekoodia

fix var dependency over properties (#3588)

benedikt-bartscher 10 kuukautta sitten
vanhempi
säilyke
a6bdaf1bbe
2 muutettua tiedostoa jossa 29 lisäystä ja 3 poistoa
  1. 15 2
      reflex/vars.py
  2. 14 1
      tests/test_state.py

+ 15 - 2
reflex/vars.py

@@ -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

+ 14 - 1
tests/test_state.py

@@ -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"}