Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve state hierarchy validation, drop old testing special case #3894

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,12 @@ def __init__(self, **kwargs):
"`connect_error_component` is deprecated, use `overlay_component` instead"
)
super().__init__(**kwargs)
base_state_subclasses = BaseState.__subclasses__()

# Special case to allow test cases have multiple subclasses of rx.BaseState.
if not is_testing_env() and len(base_state_subclasses) > 1:
# Only one Base State class is allowed.
if not is_testing_env() and BaseState.__subclasses__() != [State]:
# Only rx.State is allowed as Base State subclass.
raise ValueError(
"rx.BaseState cannot be subclassed multiple times. use rx.State instead"
"rx.BaseState cannot be subclassed directly. Use rx.State instead"
)

if "breakpoints" in self.style:
Expand Down
20 changes: 5 additions & 15 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,21 +494,11 @@ def __init_subclass__(cls, mixin: bool = False, **kwargs):
if cls.get_name() in set(
c.get_name() for c in parent_state.class_subclasses
):
if is_testing_env():
# Clear existing subclass with same name when app is reloaded via
# utils.prerequisites.get_app(reload=True)
parent_state.class_subclasses = set(
c
for c in parent_state.class_subclasses
if c.get_name() != cls.get_name()
)
else:
# During normal operation, subclasses cannot have the same name, even if they are
# defined in different modules.
raise StateValueError(
f"The substate class '{cls.get_name()}' has been defined multiple times. "
"Shadowing substate classes is not allowed."
)
# This should not happen, since we have added module prefix to state names in #3214
raise StateValueError(
f"The substate class '{cls.get_name()}' has been defined multiple times. "
"Shadowing substate classes is not allowed."
)
# Track this new subclass in the parent state's subclasses set.
parent_state.class_subclasses.add(cls)

Expand Down
Loading