Skip to content

Commit c661669

Browse files
fix: prevent AttributeError when using bare mixins (#5011)
1 parent 6d49cfb commit c661669

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

reflex/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ def get_parent_state(cls) -> Type[BaseState] | None:
907907
raise ValueError(f"Only one parent state is allowed {parent_states}.")
908908
# The first non-mixin state in the mro is our parent.
909909
for base in cls.mro()[1:]:
910-
if base._mixin or not issubclass(base, BaseState):
910+
if not issubclass(base, BaseState) or base._mixin:
911911
continue
912912
if base is BaseState:
913913
break

tests/units/test_state.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,6 +3436,30 @@ class GrandchildUsesMixinState(ChildMixinState):
34363436
pass
34373437

34383438

3439+
class BareMixin:
3440+
"""A bare mixin which does not inherit from rx.State."""
3441+
3442+
_bare_mixin: int = 0
3443+
3444+
3445+
class BareStateMixin(BareMixin, rx.State, mixin=True):
3446+
"""A state mixin that uses a bare mixin."""
3447+
3448+
pass
3449+
3450+
3451+
class BareMixinState(BareStateMixin, State):
3452+
"""A state that uses a bare mixin."""
3453+
3454+
pass
3455+
3456+
3457+
class ChildBareMixinState(BareMixinState):
3458+
"""A child state that uses a bare mixin."""
3459+
3460+
pass
3461+
3462+
34393463
def test_mixin_state() -> None:
34403464
"""Test that a mixin state works correctly."""
34413465
assert "num" in UsesMixinState.base_vars
@@ -3481,6 +3505,21 @@ def test_grandchild_mixin_state() -> None:
34813505
assert GrandchildUsesMixinState.get_root_state() == State
34823506

34833507

3508+
def test_bare_mixin_state() -> None:
3509+
"""Test that a mixin can inherit from a concrete state class."""
3510+
assert "_bare_mixin" not in BareMixinState.inherited_vars
3511+
assert "_bare_mixin" not in BareMixinState.base_vars
3512+
3513+
assert BareMixinState.get_parent_state() == State
3514+
assert BareMixinState.get_root_state() == State
3515+
3516+
assert "_bare_mixin" not in ChildBareMixinState.inherited_vars
3517+
assert "_bare_mixin" not in ChildBareMixinState.base_vars
3518+
3519+
assert ChildBareMixinState.get_parent_state() == BareMixinState
3520+
assert ChildBareMixinState.get_root_state() == State
3521+
3522+
34843523
def test_assignment_to_undeclared_vars():
34853524
"""Test that an attribute error is thrown when undeclared vars are set."""
34863525

0 commit comments

Comments
 (0)