Deprecate cached var (#3582)
* deprecate cached_var partial * migrate cached_var to var(cache=True) * fix darglint (typo)
This commit is contained in:
parent
1ca0a27d4d
commit
a7e4594fdc
@ -22,21 +22,15 @@ def ComputedVars():
|
|||||||
count: int = 0
|
count: int = 0
|
||||||
|
|
||||||
# cached var with dep on count
|
# cached var with dep on count
|
||||||
@rx.cached_var(interval=15)
|
@rx.var(cache=True, interval=15)
|
||||||
def count1(self) -> int:
|
def count1(self) -> int:
|
||||||
return self.count
|
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
|
# explicit disabled auto_deps
|
||||||
@rx.var(interval=15, cache=True, auto_deps=False)
|
@rx.var(interval=15, cache=True, auto_deps=False)
|
||||||
def count3(self) -> int:
|
def count3(self) -> int:
|
||||||
# this will not add deps, because auto_deps is False
|
# this will not add deps, because auto_deps is False
|
||||||
print(self.count1)
|
print(self.count1)
|
||||||
print(self.count2)
|
|
||||||
|
|
||||||
return self.count
|
return self.count
|
||||||
|
|
||||||
@ -76,8 +70,6 @@ def ComputedVars():
|
|||||||
rx.text(State.count, id="count"),
|
rx.text(State.count, id="count"),
|
||||||
rx.text("count1:"),
|
rx.text("count1:"),
|
||||||
rx.text(State.count1, id="count1"),
|
rx.text(State.count1, id="count1"),
|
||||||
rx.text("count2:"),
|
|
||||||
rx.text(State.count2, id="count2"),
|
|
||||||
rx.text("count3:"),
|
rx.text("count3:"),
|
||||||
rx.text(State.count3, id="count3"),
|
rx.text(State.count3, id="count3"),
|
||||||
rx.text("depends_on_count:"),
|
rx.text("depends_on_count:"),
|
||||||
@ -184,10 +176,6 @@ def test_computed_vars(
|
|||||||
assert count1
|
assert count1
|
||||||
assert count1.text == "0"
|
assert count1.text == "0"
|
||||||
|
|
||||||
count2 = driver.find_element(By.ID, "count2")
|
|
||||||
assert count2
|
|
||||||
assert count2.text == "0"
|
|
||||||
|
|
||||||
count3 = driver.find_element(By.ID, "count3")
|
count3 = driver.find_element(By.ID, "count3")
|
||||||
assert count3
|
assert count3
|
||||||
assert count3.text == "0"
|
assert count3.text == "0"
|
||||||
@ -215,7 +203,6 @@ def test_computed_vars(
|
|||||||
increment.click()
|
increment.click()
|
||||||
assert computed_vars.poll_for_content(count, timeout=2, exp_not_equal="0") == "1"
|
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(count1, timeout=2, exp_not_equal="0") == "1"
|
||||||
assert computed_vars.poll_for_content(count2, timeout=2, exp_not_equal="0") == "1"
|
|
||||||
assert (
|
assert (
|
||||||
computed_vars.poll_for_content(depends_on_count, timeout=2, exp_not_equal="0")
|
computed_vars.poll_for_content(depends_on_count, timeout=2, exp_not_equal="0")
|
||||||
== "1"
|
== "1"
|
||||||
|
@ -22,31 +22,31 @@ def MediaApp():
|
|||||||
img.format = format # type: ignore
|
img.format = format # type: ignore
|
||||||
return img
|
return img
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_default(self) -> Image.Image:
|
def img_default(self) -> Image.Image:
|
||||||
return self._blue()
|
return self._blue()
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_bmp(self) -> Image.Image:
|
def img_bmp(self) -> Image.Image:
|
||||||
return self._blue(format="BMP")
|
return self._blue(format="BMP")
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_jpg(self) -> Image.Image:
|
def img_jpg(self) -> Image.Image:
|
||||||
return self._blue(format="JPEG")
|
return self._blue(format="JPEG")
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_png(self) -> Image.Image:
|
def img_png(self) -> Image.Image:
|
||||||
return self._blue(format="PNG")
|
return self._blue(format="PNG")
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_gif(self) -> Image.Image:
|
def img_gif(self) -> Image.Image:
|
||||||
return self._blue(format="GIF")
|
return self._blue(format="GIF")
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_webp(self) -> Image.Image:
|
def img_webp(self) -> Image.Image:
|
||||||
return self._blue(format="WEBP")
|
return self._blue(format="WEBP")
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def img_from_url(self) -> Image.Image:
|
def img_from_url(self) -> Image.Image:
|
||||||
img_url = "https://picsum.photos/id/1/200/300"
|
img_url = "https://picsum.photos/id/1/200/300"
|
||||||
img_resp = httpx.get(img_url, follow_redirects=True)
|
img_resp = httpx.get(img_url, follow_redirects=True)
|
||||||
|
@ -2220,6 +2220,7 @@ def computed_var(
|
|||||||
deps: Optional[List[Union[str, Var]]] = None,
|
deps: Optional[List[Union[str, Var]]] = None,
|
||||||
auto_deps: bool = True,
|
auto_deps: bool = True,
|
||||||
interval: Optional[Union[datetime.timedelta, int]] = None,
|
interval: Optional[Union[datetime.timedelta, int]] = None,
|
||||||
|
_deprecated_cached_var: bool = False,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> ComputedVar | Callable[[Callable[[BaseState], Any]], ComputedVar]:
|
) -> ComputedVar | Callable[[Callable[[BaseState], Any]], ComputedVar]:
|
||||||
"""A ComputedVar decorator with or without kwargs.
|
"""A ComputedVar decorator with or without kwargs.
|
||||||
@ -2231,6 +2232,7 @@ def computed_var(
|
|||||||
deps: Explicit var dependencies to track.
|
deps: Explicit var dependencies to track.
|
||||||
auto_deps: Whether var dependencies should be auto-determined.
|
auto_deps: Whether var dependencies should be auto-determined.
|
||||||
interval: Interval at which the computed var should be updated.
|
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
|
**kwargs: additional attributes to set on the instance
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -2240,6 +2242,14 @@ def computed_var(
|
|||||||
ValueError: If caching is disabled and an update interval is set.
|
ValueError: If caching is disabled and an update interval is set.
|
||||||
VarDependencyError: If user supplies dependencies without caching.
|
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:
|
if cache is False and interval is not None:
|
||||||
raise ValueError("Cannot set update interval without caching.")
|
raise ValueError("Cannot set update interval without caching.")
|
||||||
|
|
||||||
@ -2264,7 +2274,7 @@ def computed_var(
|
|||||||
|
|
||||||
|
|
||||||
# Partial function of computed_var with cache=True
|
# 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):
|
class CallableVar(BaseVar):
|
||||||
|
@ -164,7 +164,7 @@ class GrandchildState(ChildState):
|
|||||||
class GrandchildState2(ChildState2):
|
class GrandchildState2(ChildState2):
|
||||||
"""A grandchild state fixture."""
|
"""A grandchild state fixture."""
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def cached(self) -> str:
|
def cached(self) -> str:
|
||||||
"""A cached var.
|
"""A cached var.
|
||||||
|
|
||||||
@ -907,7 +907,7 @@ class InterdependentState(BaseState):
|
|||||||
v1: int = 0
|
v1: int = 0
|
||||||
_v2: int = 1
|
_v2: int = 1
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def v1x2(self) -> int:
|
def v1x2(self) -> int:
|
||||||
"""Depends on var v1.
|
"""Depends on var v1.
|
||||||
|
|
||||||
@ -916,7 +916,7 @@ class InterdependentState(BaseState):
|
|||||||
"""
|
"""
|
||||||
return self.v1 * 2
|
return self.v1 * 2
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def v2x2(self) -> int:
|
def v2x2(self) -> int:
|
||||||
"""Depends on backend var _v2.
|
"""Depends on backend var _v2.
|
||||||
|
|
||||||
@ -925,7 +925,7 @@ class InterdependentState(BaseState):
|
|||||||
"""
|
"""
|
||||||
return self._v2 * 2
|
return self._v2 * 2
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def v1x2x2(self) -> int:
|
def v1x2x2(self) -> int:
|
||||||
"""Depends on ComputedVar v1x2.
|
"""Depends on ComputedVar v1x2.
|
||||||
|
|
||||||
@ -934,7 +934,7 @@ class InterdependentState(BaseState):
|
|||||||
"""
|
"""
|
||||||
return self.v1x2 * 2 # type: ignore
|
return self.v1x2 * 2 # type: ignore
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def _v3(self) -> int:
|
def _v3(self) -> int:
|
||||||
"""Depends on backend var _v2.
|
"""Depends on backend var _v2.
|
||||||
|
|
||||||
@ -943,7 +943,7 @@ class InterdependentState(BaseState):
|
|||||||
"""
|
"""
|
||||||
return self._v2
|
return self._v2
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def v3x2(self) -> int:
|
def v3x2(self) -> int:
|
||||||
"""Depends on ComputedVar _v3.
|
"""Depends on ComputedVar _v3.
|
||||||
|
|
||||||
@ -1128,7 +1128,7 @@ def test_computed_var_cached():
|
|||||||
class ComputedState(BaseState):
|
class ComputedState(BaseState):
|
||||||
v: int = 0
|
v: int = 0
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_v(self) -> int:
|
def comp_v(self) -> int:
|
||||||
nonlocal comp_v_calls
|
nonlocal comp_v_calls
|
||||||
comp_v_calls += 1
|
comp_v_calls += 1
|
||||||
@ -1148,7 +1148,7 @@ def test_computed_var_cached():
|
|||||||
|
|
||||||
|
|
||||||
def test_computed_var_cached_depends_on_non_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):
|
class ComputedState(BaseState):
|
||||||
v: int = 0
|
v: int = 0
|
||||||
@ -1157,11 +1157,11 @@ def test_computed_var_cached_depends_on_non_cached():
|
|||||||
def no_cache_v(self) -> int:
|
def no_cache_v(self) -> int:
|
||||||
return self.v
|
return self.v
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def dep_v(self) -> int:
|
def dep_v(self) -> int:
|
||||||
return self.no_cache_v # type: ignore
|
return self.no_cache_v # type: ignore
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_v(self) -> int:
|
def comp_v(self) -> int:
|
||||||
return self.v
|
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():
|
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
|
counter = 0
|
||||||
|
|
||||||
class ParentState(BaseState):
|
class ParentState(BaseState):
|
||||||
@ -1200,7 +1200,7 @@ def test_computed_var_depends_on_parent_non_cached():
|
|||||||
return counter
|
return counter
|
||||||
|
|
||||||
class ChildState(ParentState):
|
class ChildState(ParentState):
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def dep_v(self) -> int:
|
def dep_v(self) -> int:
|
||||||
return self.no_cache_v # type: ignore
|
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])
|
@pytest.mark.parametrize("use_partial", [True, False])
|
||||||
def test_cached_var_depends_on_event_handler(use_partial: bool):
|
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:
|
Args:
|
||||||
use_partial: if true, replace the EventHandler with functools.partial
|
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):
|
def handler(self):
|
||||||
self.x = self.x + 1
|
self.x = self.x + 1
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def cached_x_side_effect(self) -> int:
|
def cached_x_side_effect(self) -> int:
|
||||||
self.handler()
|
self.handler()
|
||||||
nonlocal counter
|
nonlocal counter
|
||||||
@ -1278,7 +1278,7 @@ def test_computed_var_dependencies():
|
|||||||
y: List[int] = [1, 2, 3]
|
y: List[int] = [1, 2, 3]
|
||||||
_z: List[int] = [1, 2, 3]
|
_z: List[int] = [1, 2, 3]
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_v(self) -> int:
|
def comp_v(self) -> int:
|
||||||
"""Direct access.
|
"""Direct access.
|
||||||
|
|
||||||
@ -1287,7 +1287,7 @@ def test_computed_var_dependencies():
|
|||||||
"""
|
"""
|
||||||
return self.v
|
return self.v
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_w(self):
|
def comp_w(self):
|
||||||
"""Nested lambda.
|
"""Nested lambda.
|
||||||
|
|
||||||
@ -1296,7 +1296,7 @@ def test_computed_var_dependencies():
|
|||||||
"""
|
"""
|
||||||
return lambda: self.w
|
return lambda: self.w
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_x(self):
|
def comp_x(self):
|
||||||
"""Nested function.
|
"""Nested function.
|
||||||
|
|
||||||
@ -1309,7 +1309,7 @@ def test_computed_var_dependencies():
|
|||||||
|
|
||||||
return _
|
return _
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_y(self) -> List[int]:
|
def comp_y(self) -> List[int]:
|
||||||
"""Comprehension iterating over attribute.
|
"""Comprehension iterating over attribute.
|
||||||
|
|
||||||
@ -1318,7 +1318,7 @@ def test_computed_var_dependencies():
|
|||||||
"""
|
"""
|
||||||
return [round(y) for y in self.y]
|
return [round(y) for y in self.y]
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def comp_z(self) -> List[bool]:
|
def comp_z(self) -> List[bool]:
|
||||||
"""Comprehension accesses attribute.
|
"""Comprehension accesses attribute.
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class SubA_A_A_A(SubA_A_A):
|
|||||||
class SubA_A_A_B(SubA_A_A):
|
class SubA_A_A_B(SubA_A_A):
|
||||||
"""SubA_A_A_B is a child of 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:
|
def sub_a_a_a_cached(self) -> int:
|
||||||
"""A cached var.
|
"""A cached var.
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ class SubE_A_A_A_D(SubE_A_A_A):
|
|||||||
|
|
||||||
sub_e_a_a_a_d: int
|
sub_e_a_a_a_d: int
|
||||||
|
|
||||||
@rx.cached_var
|
@rx.var(cache=True)
|
||||||
def sub_e_a_a_a_d_var(self) -> int:
|
def sub_e_a_a_a_d_var(self) -> int:
|
||||||
"""A computed var.
|
"""A computed var.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user