Reject clashing mixin vars; let mixin vars without a default use the parent's

Merged
MA
1d ago
+660-14

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

Picks up #7206 by @qubydev, which its author closed, keeping their commits and tests. Refs #7190, #7312.

Tightens how state mixins declare vars, on top of the field binding from #7312:

  • Conflicting mixins (breaking, for 0.11.0): two mixins that do not inherit from one another can no longer both declare a var of the same name. Creating the state or mixin that combines them raises the new MixinVarNameConflictError. A state is meant to combine its mixins into one coherent state, and two independent mixins sharing a var name could each use it for their own purpose. Backend vars count too. A var the combining state declares itself settles the conflict: both mixins use it, since mixins from different libraries cannot be renamed. Mixins already combined into the parent state are exempt: they were checked with it, and a substate may redeclare its vars.
  • Typing a var declared further along: a mixin may declare a var without a default (count: int) when the combining state, its parent state or another mixin with a default declares that var. The declaration only types the var for the mixin's code, and Reflex warns when the types do not match. When nothing else declares the var, the mixins each expect to own it, and combining them raises.
  • Bug fix: a var a mixin declared without a default replaced the var of that name declared by a parent state with an independent one, defaulting to the type's zero value. It now uses the var declared further along the MRO, as re-annotating it on the substate does: the parent state's, or one a mixin declares with a default. Re-annotating such a var with another type (x: str over a mixin's x: int) now gives it that type's default instead of 0.
  • Deprecated exception: BaseVarShadowsInheritedVarError, unused since #7312, is deprecated: importing it warns, and it will be removed in 1.0.

Redeclaring an inherited var with a default, directly or through a mixin, still gives the substate an independent var, as on main.

Changes

  • reflex_base.vars.base:
    • Field._default_from_type notes a declaration without a default. _replace derives such a default again from the (new) type, and set_default clears the flag.
    • _declared_value looks up a var's declaration, passing over a mixin's var without a default for one declared further along the MRO. _annotated_fields and BaseState._bind_fields use it, and _bind_fields exposes a parent state's var on the class with type.__setattr__, where the mixin's would hide it. It skips a ClassVar overriding an inherited var.
    • BaseStateMeta runs _validate_mixin_vars, which raises MixinVarNameConflictError unless the state declares the var itself, and warns when a mixin's var without a default is typed other than the var _declared_value finds.
  • reflex_base.utils.exceptions: adds MixinVarNameConflictError; BaseVarShadowsInheritedVarError stays importable through a module __getattr__ that calls console.deprecate, which reflex.utils.exceptions forwards to.
  • docs/state_structure/mixins.md documents both rules and drops the claim that conflicting mixin vars are resolved by MRO.
  • News fragments: a breaking change and a bug fix for reflex, and a breaking change and a deprecation for reflex-base.

Tests

  • tests/units/test_state.py: the mixin tests assert which declaration wins. A mixin's var without a default keeps the parent's var, including when the parent got it from a mixin and when the child re-annotates it.
  • tests/units/reflex_base/vars/test_base.py: conflicts, compared in full, when combining into a substate, a new root state and a mixin, for backend vars and for declarations without a default. Mixins may type a var the parent or the state declares, and do not conflict with the parent's mixins. A state declaring the var settles its mixins' conflict. A re-annotated var takes the new type's default. The Field flag, including after set_default. A mixin's var without a default uses another mixin's var with a default, with or without a parent var, and warns on another type.
  • tests/units/reflex_base/utils/test_exceptions.py: importing BaseVarShadowsInheritedVarError warns.

Validation

  • uv run pytest tests/units: 11151 passed. The browser-driver tests need a Playwright build missing locally and fail the same way without this change.
  • uv run ruff check ., uv run ruff format --check ., uv run pyright reflex tests and the changelog check pass.

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo


Generated by Claude Code