From 15af1cade4117a8bb590b2ecade626aca74bd226 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 26 Sep 2024 17:28:37 +0000 Subject: [PATCH] restrict this only to frontend vars for now. --- reflex/state.py | 4 +++- tests/units/test_state.py | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/reflex/state.py b/reflex/state.py index cfe820141..95a767fea 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -1281,7 +1281,9 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): return if ( - not name.startswith("__") + not name.startswith( + "_" + ) # TODO: skipping backend vars and vars with double leading underscores for now. They should be supported, however. and name not in self.vars and name not in self.get_skip_vars() ): diff --git a/tests/units/test_state.py b/tests/units/test_state.py index 1f5c6a70c..06362f443 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -3269,10 +3269,23 @@ def test_assignment_to_undeclared_vars(): class State(BaseState): val: str + _val: str + __val: str - def handle(self): + def handle_supported_regular_vars(self): + self.val = "no underscore" + self._val = "single leading underscore" + self.__val = "double leading undercore" + + def handle_regular_var(self): self.num = 5 + def handle_backend_var(self): + self._num = 5 + + def handle_non_var(self): + self.__num = 5 + class Substate(State): def handle_var(self): self.value = 20 @@ -3281,7 +3294,16 @@ def test_assignment_to_undeclared_vars(): sub_state = Substate() # type: ignore with pytest.raises(AttributeError): - state.handle() + state.handle_regular_var() with pytest.raises(AttributeError): sub_state.handle_var() + + # TODO: uncomment this if the case of backend vars are supported. + # with pytest.raises(AttributeError): + # state.handle_backend_var() + # + # with pytest.raises(AttributeError): + # state.handle_non_var() + + state.handle_supported_regular_vars()