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

View File

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

View File

@ -10,6 +10,7 @@ from functools import wraps
from typing import (
Any,
Callable,
ClassVar,
Dict,
Iterable,
List,
@ -413,9 +414,22 @@ def is_backend_variable(name: str, cls: Type | None = None) -> bool:
Returns:
bool: The result of the check
"""
if cls is not None and name.startswith(f"_{cls.__name__}__"):
if not name.startswith("_"):
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:

View File

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