Warn when a base var shadows a var inherited from a parent state

Merged
BL
3mo ago
+130-1

Problem

Fixes #7074. When a substate declares a field whose name matches a base var it inherits, the declaration is discarded with no warning and no error. get_skip_vars() includes set(cls.inherited_vars) and base_vars filters on it, so the field stays in get_fields() but never becomes a base var: _init_var never runs, class-level access returns pydantic's raw default instead of a Var, and instance reads and writes are delegated to the parent.

The reporter's repro, unchanged:

class Parent(State):
    x: int = 1

class Child(Parent):
    x: str = "ninety-nine"   # silently ignored

child.x is 1 (an int), a write to child.x lands on parent.x, child.dirty_vars stays empty, and repr(Child.x) is a plain str — so rx.text(Child.x) compiles a literal into the page instead of a reactive binding.

Change

BaseState._check_overridden_inherited_vars() runs at class creation and emits a deduped console.warn naming the var and both states. A field redeclared on a class is a distinct Field object from the parent's, while a merely inherited one is the same object — that identity check is the discriminator, since on Python 3.14 a bare annotation carries no marker distinguishing it from an explicit assignment (__annotations__ is not even populated in cls.__dict__).

Why warn instead of raise

The issue asks for a raise, and the shadowing computed var guards do raise (ComputedVarShadowsStateVarError). I went with a warning because CLAUDE.md says "Reflex has downstream users — don't break them", and raising turns code that runs today into an import-time crash. The warning fully addresses the reported failure mode (silence), and escalating to an exception later is a small change. Happy to switch it if you'd rather have the hard error.

One deliberate exemption

tests/units/vars/test_hybrid_property.py::test_hybrid_property_shadowed_by_closer_base_stays_a_field re-annotates an inherited var on purpose, in a hierarchy where the state field already outranks a hybrid_property reached through a non-state base. A redeclaration is therefore skipped only when both a same-named non-state descriptor exists in the MRO and a state base declaring the name precedes it — the ordering in which the field wins anyway, making the re-annotation inert. With no descriptor in the MRO, an ordinary redeclaration still warns.

_is_user_descriptor gained an include_properties keyword for this (default False, so the existing call site is unchanged) because HybridProperty subclasses property, which that helper otherwise excludes.

The resulting invariant: the warning fires exactly when the shadowed name stops resolving to a Var at class level, which is the case that actually breaks a component. See the table in the review thread for the three hierarchies this was measured against.

Incidental

DynamicState in tests/units/test_app.py declared is_hydrated: bool = False, shadowing the root State var of the same name and identical default — already a no-op, and the first thing the new warning flagged. Removed.

Testing

Three tests in tests/units/test_state.py:

  • test_base_var_shadowing_inherited_var_warns — fails before this change, passes after.
  • test_base_var_shadowing_non_state_descriptor_does_not_warn — guards the exemption so the warning can't over-fire on the legitimate MRO pattern.
  • test_base_var_shadowing_warns_when_descriptor_outranks_state_field — added after review, locks out the false negative where a descriptor precedes the state field.

uv run pytest tests/units → 8442 passed, 18 skipped. ruff check, ruff format --check and pyright reflex/state.py tests/units/test_state.py are clean. News fragment added at news/7074.bugfix.md.