|
@@ -87,6 +87,7 @@ from reflex.utils.exceptions import (
|
|
ImmutableStateError,
|
|
ImmutableStateError,
|
|
InvalidStateManagerMode,
|
|
InvalidStateManagerMode,
|
|
LockExpiredError,
|
|
LockExpiredError,
|
|
|
|
+ ReflexRuntimeError,
|
|
SetUndefinedStateVarError,
|
|
SetUndefinedStateVarError,
|
|
StateSchemaMismatchError,
|
|
StateSchemaMismatchError,
|
|
)
|
|
)
|
|
@@ -387,6 +388,10 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
"State classes should not be instantiated directly in a Reflex app. "
|
|
"State classes should not be instantiated directly in a Reflex app. "
|
|
"See https://reflex.dev/docs/state/ for further information."
|
|
"See https://reflex.dev/docs/state/ for further information."
|
|
)
|
|
)
|
|
|
|
+ if type(self)._mixin:
|
|
|
|
+ raise ReflexRuntimeError(
|
|
|
|
+ f"{type(self).__name__} is a state mixin and cannot be instantiated directly."
|
|
|
|
+ )
|
|
kwargs["parent_state"] = parent_state
|
|
kwargs["parent_state"] = parent_state
|
|
super().__init__()
|
|
super().__init__()
|
|
for name, value in kwargs.items():
|
|
for name, value in kwargs.items():
|
|
@@ -2367,6 +2372,23 @@ class ComponentState(State, mixin=True):
|
|
# The number of components created from this class.
|
|
# The number of components created from this class.
|
|
_per_component_state_instance_count: ClassVar[int] = 0
|
|
_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 state via the `State` attribute."
|
|
|
|
+ )
|
|
|
|
+ super().__init__(*args, **kwargs)
|
|
|
|
+
|
|
@classmethod
|
|
@classmethod
|
|
def __init_subclass__(cls, mixin: bool = True, **kwargs):
|
|
def __init_subclass__(cls, mixin: bool = True, **kwargs):
|
|
"""Overwrite mixin default to True.
|
|
"""Overwrite mixin default to True.
|