copy backend vars from mixins (#3580)

* copy backend vars from mixins

* fix and improve backend var mixin test
This commit is contained in:
benedikt-bartscher 2024-06-28 19:42:41 +02:00 committed by GitHub
parent a7e4594fdc
commit 33d7ec1f04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 0 deletions

View File

@ -200,6 +200,7 @@ def _no_chain_background_task(
RESERVED_BACKEND_VAR_NAMES = {
"_abc_impl",
"_backend_vars",
"_was_touched",
}
@ -558,6 +559,14 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
cls.computed_vars[newcv._var_name] = newcv
cls.vars[newcv._var_name] = newcv
continue
if (
types.is_backend_variable(name, cls)
and name not in RESERVED_BACKEND_VAR_NAMES
and name not in cls.inherited_backend_vars
and not isinstance(value, FunctionType)
):
cls.backend_vars[name] = copy.deepcopy(value)
continue
if events.get(name) is not None:
continue
if not cls._item_is_event_handler(name, value):

View File

@ -2969,3 +2969,23 @@ config = rx.Config(
state_manager = StateManager.create(state=State)
assert state_manager.lock_expiration == expected_values[0] # type: ignore
assert state_manager.token_expiration == expected_values[1] # type: ignore
class MixinState(State, mixin=True):
"""A mixin state for testing."""
num: int = 0
_backend: int = 0
class UsesMixinState(MixinState, State):
"""A state that uses the mixin state."""
pass
def test_mixin_state() -> None:
"""Test that a mixin state works correctly."""
assert "num" in UsesMixinState.base_vars
assert "num" in UsesMixinState.vars
assert UsesMixinState.backend_vars == {"_backend": 0}