From b34a5d3b5455ec642f0a875145b9ecaaebb27001 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 26 Sep 2024 11:44:56 +0000 Subject: [PATCH] `self.var` contains all the info we need --- reflex/state.py | 6 ++++-- tests/units/test_state.py | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/reflex/state.py b/reflex/state.py index d7cfcf18b..ab22fd688 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -1277,8 +1277,10 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): self._mark_dirty() return - if not name in self.vars and not name in self._computed_var_dependencies and not name in self.get_skip_vars(): - raise AttributeError(f"The state var '{name}' has not been defined in '{type(self).__name__}'. All state vars must be declared before they can be set.") + if name not in self.vars and name not in self.get_skip_vars(): + raise AttributeError( + f"The state var '{name}' has not been defined in '{type(self).__name__}'. All state vars must be declared before they can be set." + ) # Set the attribute. super().__setattr__(name, value) diff --git a/tests/units/test_state.py b/tests/units/test_state.py index b85116101..d4ece2b81 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -3265,7 +3265,8 @@ def test_child_mixin_state() -> None: def test_assignment_to_undeclared_vars(): - """Test that an attribute error is thrown when undeclared vars are set""" + """Test that an attribute error is thrown when undeclared vars are set.""" + class State(BaseState): val: str @@ -3273,7 +3274,6 @@ def test_assignment_to_undeclared_vars(): self.num = 5 class Substate(State): - def handle_var(self): self.value = 20 @@ -3285,3 +3285,6 @@ def test_assignment_to_undeclared_vars(): with pytest.raises(AttributeError): sub_state.handle() + + with pytest.raises(AttributeError): + sub_state.handle_var()