Setting State Vars that are not defined should raise an error
This commit is contained in:
parent
c08720ed1a
commit
ecb9f141e4
@ -1277,6 +1277,9 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|||||||
self._mark_dirty()
|
self._mark_dirty()
|
||||||
return
|
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.")
|
||||||
|
|
||||||
# Set the attribute.
|
# Set the attribute.
|
||||||
super().__setattr__(name, value)
|
super().__setattr__(name, value)
|
||||||
|
|
||||||
|
@ -3262,3 +3262,26 @@ def test_child_mixin_state() -> None:
|
|||||||
|
|
||||||
assert "computed" in ChildUsesMixinState.inherited_vars
|
assert "computed" in ChildUsesMixinState.inherited_vars
|
||||||
assert "computed" not in ChildUsesMixinState.computed_vars
|
assert "computed" not in ChildUsesMixinState.computed_vars
|
||||||
|
|
||||||
|
|
||||||
|
def test_assignment_to_undeclared_vars():
|
||||||
|
"""Test that an attribute error is thrown when undeclared vars are set"""
|
||||||
|
class State(BaseState):
|
||||||
|
val: str
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
self.num = 5
|
||||||
|
|
||||||
|
class Substate(State):
|
||||||
|
|
||||||
|
def handle_var(self):
|
||||||
|
self.value = 20
|
||||||
|
|
||||||
|
state = State()
|
||||||
|
sub_state = Substate()
|
||||||
|
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
state.handle()
|
||||||
|
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
sub_state.handle()
|
||||||
|
Loading…
Reference in New Issue
Block a user