Deprecate cached var (#3582)

* deprecate cached_var partial

* migrate cached_var to var(cache=True)

* fix darglint (typo)
This commit is contained in:
benedikt-bartscher 2024-06-28 19:14:55 +02:00 committed by GitHub
parent 1ca0a27d4d
commit a7e4594fdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 43 deletions

View File

@ -22,21 +22,15 @@ def ComputedVars():
count: int = 0
# cached var with dep on count
@rx.cached_var(interval=15)
@rx.var(cache=True, interval=15)
def count1(self) -> int:
return self.count
# same as above, different notation
@rx.var(interval=15, cache=True)
def count2(self) -> int:
return self.count
# explicit disabled auto_deps
@rx.var(interval=15, cache=True, auto_deps=False)
def count3(self) -> int:
# this will not add deps, because auto_deps is False
print(self.count1)
print(self.count2)
return self.count
@ -76,8 +70,6 @@ def ComputedVars():
rx.text(State.count, id="count"),
rx.text("count1:"),
rx.text(State.count1, id="count1"),
rx.text("count2:"),
rx.text(State.count2, id="count2"),
rx.text("count3:"),
rx.text(State.count3, id="count3"),
rx.text("depends_on_count:"),
@ -184,10 +176,6 @@ def test_computed_vars(
assert count1
assert count1.text == "0"
count2 = driver.find_element(By.ID, "count2")
assert count2
assert count2.text == "0"
count3 = driver.find_element(By.ID, "count3")
assert count3
assert count3.text == "0"
@ -215,7 +203,6 @@ def test_computed_vars(
increment.click()
assert computed_vars.poll_for_content(count, timeout=2, exp_not_equal="0") == "1"
assert computed_vars.poll_for_content(count1, timeout=2, exp_not_equal="0") == "1"
assert computed_vars.poll_for_content(count2, timeout=2, exp_not_equal="0") == "1"
assert (
computed_vars.poll_for_content(depends_on_count, timeout=2, exp_not_equal="0")
== "1"

View File

@ -22,31 +22,31 @@ def MediaApp():
img.format = format # type: ignore
return img
@rx.cached_var
@rx.var(cache=True)
def img_default(self) -> Image.Image:
return self._blue()
@rx.cached_var
@rx.var(cache=True)
def img_bmp(self) -> Image.Image:
return self._blue(format="BMP")
@rx.cached_var
@rx.var(cache=True)
def img_jpg(self) -> Image.Image:
return self._blue(format="JPEG")
@rx.cached_var
@rx.var(cache=True)
def img_png(self) -> Image.Image:
return self._blue(format="PNG")
@rx.cached_var
@rx.var(cache=True)
def img_gif(self) -> Image.Image:
return self._blue(format="GIF")
@rx.cached_var
@rx.var(cache=True)
def img_webp(self) -> Image.Image:
return self._blue(format="WEBP")
@rx.cached_var
@rx.var(cache=True)
def img_from_url(self) -> Image.Image:
img_url = "https://picsum.photos/id/1/200/300"
img_resp = httpx.get(img_url, follow_redirects=True)

View File

@ -2220,6 +2220,7 @@ def computed_var(
deps: Optional[List[Union[str, Var]]] = None,
auto_deps: bool = True,
interval: Optional[Union[datetime.timedelta, int]] = None,
_deprecated_cached_var: bool = False,
**kwargs,
) -> ComputedVar | Callable[[Callable[[BaseState], Any]], ComputedVar]:
"""A ComputedVar decorator with or without kwargs.
@ -2231,6 +2232,7 @@ def computed_var(
deps: Explicit var dependencies to track.
auto_deps: Whether var dependencies should be auto-determined.
interval: Interval at which the computed var should be updated.
_deprecated_cached_var: Indicate usage of deprecated cached_var partial function.
**kwargs: additional attributes to set on the instance
Returns:
@ -2240,6 +2242,14 @@ def computed_var(
ValueError: If caching is disabled and an update interval is set.
VarDependencyError: If user supplies dependencies without caching.
"""
if _deprecated_cached_var:
console.deprecate(
feature_name="cached_var",
reason=("Use @rx.var(cache=True) instead of @rx.cached_var."),
deprecation_version="0.5.6",
removal_version="0.6.0",
)
if cache is False and interval is not None:
raise ValueError("Cannot set update interval without caching.")
@ -2264,7 +2274,7 @@ def computed_var(
# Partial function of computed_var with cache=True
cached_var = functools.partial(computed_var, cache=True)
cached_var = functools.partial(computed_var, cache=True, _deprecated_cached_var=True)
class CallableVar(BaseVar):

View File

@ -164,7 +164,7 @@ class GrandchildState(ChildState):
class GrandchildState2(ChildState2):
"""A grandchild state fixture."""
@rx.cached_var
@rx.var(cache=True)
def cached(self) -> str:
"""A cached var.
@ -907,7 +907,7 @@ class InterdependentState(BaseState):
v1: int = 0
_v2: int = 1
@rx.cached_var
@rx.var(cache=True)
def v1x2(self) -> int:
"""Depends on var v1.
@ -916,7 +916,7 @@ class InterdependentState(BaseState):
"""
return self.v1 * 2
@rx.cached_var
@rx.var(cache=True)
def v2x2(self) -> int:
"""Depends on backend var _v2.
@ -925,7 +925,7 @@ class InterdependentState(BaseState):
"""
return self._v2 * 2
@rx.cached_var
@rx.var(cache=True)
def v1x2x2(self) -> int:
"""Depends on ComputedVar v1x2.
@ -934,7 +934,7 @@ class InterdependentState(BaseState):
"""
return self.v1x2 * 2 # type: ignore
@rx.cached_var
@rx.var(cache=True)
def _v3(self) -> int:
"""Depends on backend var _v2.
@ -943,7 +943,7 @@ class InterdependentState(BaseState):
"""
return self._v2
@rx.cached_var
@rx.var(cache=True)
def v3x2(self) -> int:
"""Depends on ComputedVar _v3.
@ -1128,7 +1128,7 @@ def test_computed_var_cached():
class ComputedState(BaseState):
v: int = 0
@rx.cached_var
@rx.var(cache=True)
def comp_v(self) -> int:
nonlocal comp_v_calls
comp_v_calls += 1
@ -1148,7 +1148,7 @@ def test_computed_var_cached():
def test_computed_var_cached_depends_on_non_cached():
"""Test that a cached_var is recalculated if it depends on non-cached ComputedVar."""
"""Test that a cached var is recalculated if it depends on non-cached ComputedVar."""
class ComputedState(BaseState):
v: int = 0
@ -1157,11 +1157,11 @@ def test_computed_var_cached_depends_on_non_cached():
def no_cache_v(self) -> int:
return self.v
@rx.cached_var
@rx.var(cache=True)
def dep_v(self) -> int:
return self.no_cache_v # type: ignore
@rx.cached_var
@rx.var(cache=True)
def comp_v(self) -> int:
return self.v
@ -1189,7 +1189,7 @@ def test_computed_var_cached_depends_on_non_cached():
def test_computed_var_depends_on_parent_non_cached():
"""Child state cached_var that depends on parent state un cached var is always recalculated."""
"""Child state cached var that depends on parent state un cached var is always recalculated."""
counter = 0
class ParentState(BaseState):
@ -1200,7 +1200,7 @@ def test_computed_var_depends_on_parent_non_cached():
return counter
class ChildState(ParentState):
@rx.cached_var
@rx.var(cache=True)
def dep_v(self) -> int:
return self.no_cache_v # type: ignore
@ -1233,7 +1233,7 @@ def test_computed_var_depends_on_parent_non_cached():
@pytest.mark.parametrize("use_partial", [True, False])
def test_cached_var_depends_on_event_handler(use_partial: bool):
"""A cached_var that calls an event handler calculates deps correctly.
"""A cached var that calls an event handler calculates deps correctly.
Args:
use_partial: if true, replace the EventHandler with functools.partial
@ -1246,7 +1246,7 @@ def test_cached_var_depends_on_event_handler(use_partial: bool):
def handler(self):
self.x = self.x + 1
@rx.cached_var
@rx.var(cache=True)
def cached_x_side_effect(self) -> int:
self.handler()
nonlocal counter
@ -1278,7 +1278,7 @@ def test_computed_var_dependencies():
y: List[int] = [1, 2, 3]
_z: List[int] = [1, 2, 3]
@rx.cached_var
@rx.var(cache=True)
def comp_v(self) -> int:
"""Direct access.
@ -1287,7 +1287,7 @@ def test_computed_var_dependencies():
"""
return self.v
@rx.cached_var
@rx.var(cache=True)
def comp_w(self):
"""Nested lambda.
@ -1296,7 +1296,7 @@ def test_computed_var_dependencies():
"""
return lambda: self.w
@rx.cached_var
@rx.var(cache=True)
def comp_x(self):
"""Nested function.
@ -1309,7 +1309,7 @@ def test_computed_var_dependencies():
return _
@rx.cached_var
@rx.var(cache=True)
def comp_y(self) -> List[int]:
"""Comprehension iterating over attribute.
@ -1318,7 +1318,7 @@ def test_computed_var_dependencies():
"""
return [round(y) for y in self.y]
@rx.cached_var
@rx.var(cache=True)
def comp_z(self) -> List[bool]:
"""Comprehension accesses attribute.

View File

@ -42,7 +42,7 @@ class SubA_A_A_A(SubA_A_A):
class SubA_A_A_B(SubA_A_A):
"""SubA_A_A_B is a child of SubA_A_A."""
@rx.cached_var
@rx.var(cache=True)
def sub_a_a_a_cached(self) -> int:
"""A cached var.
@ -183,7 +183,7 @@ class SubE_A_A_A_D(SubE_A_A_A):
sub_e_a_a_a_d: int
@rx.cached_var
@rx.var(cache=True)
def sub_e_a_a_a_d_var(self) -> int:
"""A computed var.