Repository navigation
Reject clashing mixin vars; let mixin vars without a default use the parent's - #7381
Conversation
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
#7312 replaced the inherited-var shadow checks with per-state fields: a state that redeclares an inherited var, directly or through a mixin, now gets its own reactive var, and _check_overridden_inherited_vars is gone. The reproduction from #7190 yields a Var owned by the child that keeps the mixin's default, so there is nothing left to reject. Keep the three tests that still hold on main as regression coverage and drop the state.py change, its news fragment and the tests expecting BaseVarShadowsInheritedVarError. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
Brings in #7312, which removed _check_overridden_inherited_vars and the shadow tests this branch was built around. After the previous commit the branch only adds tests, so the merge resolves without conflicts. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
The incompatible-override error for a state inheriting the same var name from its parent and a mixin is reported on the inheriting class, not on the mixin's declaration. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
The three tests only checked that class-level access returns a Var, which holds whichever declaration wins. Check the field's owner or default instead. That shows an annotation-only mixin declaration listed before the parent replacing the inherited var with an independent one that defaults to the type's zero value, unlike a bare re-annotation on a substate, so that test is a strict xfail until main is fixed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
…parent's A state combines its mixins into one coherent state, so two mixins that do not inherit from one another and declare the same var would each use it for their own purpose. Creating the class that combines them now raises MixinVarNameConflictError. Mixins already combined into the parent state are left out: they were checked with it, and a substate may redeclare its vars. MixinVarNameConflictError replaces BaseVarShadowsInheritedVarError, which nothing raises since #7312. A var that a mixin declares without a default (`count: int`) replaced the var of that name declared by a parent state with an independent one defaulting to the type's zero value. It now restates the parent's var, as re-annotating it on the substate does: a field notes when its default comes from its type, _declared_value passes over such a mixin var for a state's var further along the MRO, and _bind_fields exposes that var on the class, where the mixin's would hide it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
A var a mixin declares without a default only types the var for the mixin's own code when the class combining it, or its parent state, declares that var, so several mixins may each do so. Without such a declaration on the state, the mixins each expect to own the var, which still raises MixinVarNameConflictError. Such a var now also yields to one a mixin declares with a default further along the MRO, not only to a parent state's var. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
[Medium impact] The PR appears safe to merge, with a non-blocking correction to the documented default requirement. Findings
SummaryThis PR rejects conflicting mixin variables and lets declarations without defaults reuse variables from a parent state or another mixin.
Reviews (5) · Last reviewed commit: "Docs: a mixin var without a default need..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 9 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Merging this PR will degrade performance by 8%3 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
The exemption for a var a mixin declares without a default looked at every name in the class body, so a ClassVar, method or computed var of that name hid a conflict between two mixins. It now counts only the fields the class declares and those of its parent state, so the check runs once the class's own fields are known. Also note in the docs that mixins a parent state already combines do not count toward a conflict. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
FarhanAliRaza
left a comment
There was a problem hiding this comment.
I tested this PR in a small Reflex app. The app has a parent state with count = 5 and _secret = 7. Mixins declare these vars without a default. The app has a child with one such mixin, a child with two such mixins, a standalone state, and a child whose mixin redeclares count = 100. I ran the same app on main as a baseline.
On main, the child gets a separate var with value 0. The mixin events do not change the parent. On this PR, the child shows 5. The mixin events change the parent var, and computed vars in the mixin update. Two mixins without a default share the parent var. The backend var _secret works in the same way. The values stay correct after a page reload, and the browser console shows no errors. The redeclared default still gives the child its own var.
I also ran class-creation probes on both branches. They covered conflicting mixins, mismatched types, mixins that subclass mixins, re-annotation in a grandchild, and computed-var dependencies. The state and var unit tests pass.
The inline comments give the requested changes.
… main merge - `_bind_fields` exposes the parent state's var with `type.__setattr__`, as `_bind_attr` does: main's `BaseStateMeta.__setattr__` refuses the plain `setattr` over the mixin's field. It does so only when the MRO lookup found a field, so a ClassVar overriding an inherited var is no longer overwritten. - A var the combining state declares itself no longer conflicts between its mixins: mixins from different libraries cannot be renamed. - `Field._replace` derives a default that came from the type again, so re-annotating a mixin's `x: int` as `x: str` defaults to "" instead of 0. `set_default` clears the flag, so a set default is kept and not skipped as a restatement.
…alias - A mixin's var without a default no longer conflicts when `_declared_value` finds a declaration further along, such as another mixin's var with a default, so the conflict check and the binding agree with or without a parent state declaring the var. - Warn when such a var is typed other than the var it uses is declared. - Keep `BaseVarShadowsInheritedVarError` importable from reflex_base and reflex for one release, with a deprecation warning. - Simplify the `_declared_value` loop to one `_restates_type` call per step. - Compare the conflict errors in full in the tests.
There was a problem hiding this comment.
All reported issues were addressed across 9 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
View guided diff | Turn on auto-fix | Re-trigger cubic
All Submissions:
Type of change
Changes To Core Features:
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:
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.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.x: strover a mixin'sx: int) now gives it that type's default instead of0.BaseVarShadowsInheritedVarError, unused since ENG-10096: Make Field, ComputedVar and EventHandler state descriptors #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_typenotes a declaration without a default._replacederives such a default again from the (new) type, andset_defaultclears the flag._declared_valuelooks up a var's declaration, passing over a mixin's var without a default for one declared further along the MRO._annotated_fieldsandBaseState._bind_fieldsuse it, and_bind_fieldsexposes a parent state's var on the class withtype.__setattr__, where the mixin's would hide it. It skips a ClassVar overriding an inherited var.BaseStateMetaruns_validate_mixin_vars, which raisesMixinVarNameConflictErrorunless the state declares the var itself, and warns when a mixin's var without a default is typed other than the var_declared_valuefinds.reflex_base.utils.exceptions: addsMixinVarNameConflictError;BaseVarShadowsInheritedVarErrorstays importable through a module__getattr__that callsconsole.deprecate, whichreflex.utils.exceptionsforwards to.docs/state_structure/mixins.mddocuments both rules and drops the claim that conflicting mixin vars are resolved by MRO.reflex, and a breaking change and a deprecation forreflex-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. TheFieldflag, including afterset_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: importingBaseVarShadowsInheritedVarErrorwarns.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 testsand the changelog check pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01NDLXDrfgjniwJLecUhHNCo
Generated by Claude Code