From a91f89aade92d8a67956ea7aa49b019b327f7f51 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Sun, 10 Nov 2024 21:15:59 +0100 Subject: [PATCH] improve error message for ComponentState mixins --- reflex/state.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/reflex/state.py b/reflex/state.py index 724aeee90..1cbac53ce 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -86,6 +86,7 @@ from reflex.utils.exceptions import ( ImmutableStateError, InvalidStateManagerMode, LockExpiredError, + ReflexRuntimeError, SetUndefinedStateVarError, StateSchemaMismatchError, ) @@ -2365,6 +2366,23 @@ class ComponentState(State, mixin=True): # The number of components created from this class. _per_component_state_instance_count: ClassVar[int] = 0 + def __init__(self, *args, **kwargs): + """Do not allow direct initialization of the ComponentState. + + Args: + *args: The args to pass to the State init method. + **kwargs: The kwargs to pass to the State init method. + + Raises: + ReflexRuntimeError: If the ComponentState is initialized directly. + """ + if type(self)._mixin: + raise ReflexRuntimeError( + f"{ComponentState.__name__} {type(self).__name__} is not meant to be initialized directly. " + + "Use the `create` method to create a new instance and access the statte via the `State` attribute." + ) + super().__init__(*args, **kwargs) + @classmethod def __init_subclass__(cls, mixin: bool = True, **kwargs): """Overwrite mixin default to True.