classvars should not be backend vars (#3578)

* classvars should not be backend vars

* cleanup RESERVED_BACKEND_VAR_NAMES
This commit is contained in:
benedikt-bartscher 2024-06-28 01:03:36 +02:00 committed by GitHub
parent 2027a2f09f
commit 8e66c9b3c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 8 deletions

View File

@ -15,7 +15,10 @@ def ComputedVars():
"""Test app for computed vars.""" """Test app for computed vars."""
import reflex as rx import reflex as rx
class State(rx.State): class StateMixin(rx.State, mixin=True):
pass
class State(StateMixin, rx.State):
count: int = 0 count: int = 0
# cached var with dep on count # cached var with dep on count
@ -57,6 +60,8 @@ def ComputedVars():
def mark_dirty(self): def mark_dirty(self):
self._mark_dirty() self._mark_dirty()
assert State.backend_vars == {}
def index() -> rx.Component: def index() -> rx.Component:
return rx.center( return rx.center(
rx.vstack( rx.vstack(

View File

@ -201,10 +201,6 @@ def _no_chain_background_task(
RESERVED_BACKEND_VAR_NAMES = { RESERVED_BACKEND_VAR_NAMES = {
"_backend_vars", "_backend_vars",
"_computed_var_dependencies",
"_substate_var_dependencies",
"_always_dirty_computed_vars",
"_always_dirty_substates",
"_was_touched", "_was_touched",
} }

View File

@ -10,6 +10,7 @@ from functools import wraps
from typing import ( from typing import (
Any, Any,
Callable, Callable,
ClassVar,
Dict, Dict,
Iterable, Iterable,
List, List,
@ -413,9 +414,22 @@ def is_backend_variable(name: str, cls: Type | None = None) -> bool:
Returns: Returns:
bool: The result of the check bool: The result of the check
""" """
if cls is not None and name.startswith(f"_{cls.__name__}__"): if not name.startswith("_"):
return False return False
return name.startswith("_") and not name.startswith("__")
if name.startswith("__"):
return False
if cls is not None:
if name.startswith(f"_{cls.__name__}__"):
return False
hints = get_type_hints(cls)
if name in hints:
hint = get_origin(hints[name])
if hint == ClassVar:
return False
return True
def check_type_in_allowed_types(value_type: Type, allowed_types: Iterable) -> bool: def check_type_in_allowed_types(value_type: Type, allowed_types: Iterable) -> bool:

View File

@ -1,7 +1,7 @@
import os import os
import typing import typing
from pathlib import Path from pathlib import Path
from typing import Any, List, Literal, Union from typing import Any, ClassVar, List, Literal, Union
import pytest import pytest
import typer import typer
@ -149,6 +149,7 @@ def test_backend_variable_cls():
class TestBackendVariable: class TestBackendVariable:
"""Test backend variable.""" """Test backend variable."""
_classvar: ClassVar[int] = 0
_hidden: int = 0 _hidden: int = 0
not_hidden: int = 0 not_hidden: int = 0
__dunderattr__: int = 0 __dunderattr__: int = 0
@ -159,6 +160,7 @@ def test_backend_variable_cls():
@pytest.mark.parametrize( @pytest.mark.parametrize(
"input, output", "input, output",
[ [
("_classvar", False),
("_hidden", True), ("_hidden", True),
("not_hidden", False), ("not_hidden", False),
("__dundermethod__", False), ("__dundermethod__", False),