Preserve state descriptors when assigning class defaults
Class assignment to a declared state var could replace its descriptor while the field registry retained the old field, causing fresh instances, reset, dirty tracking, and persistence to disagree. Frontend assignments such as cls.count = 10 in ComponentState.get_component also replaced the frontend expression.
Assigning a value now updates the existing descriptor's default consistently for frontend and backend vars. A callable the field's annotation accepts, such as for a Callable or Any var, is stored as the default. Assigning a zero-argument callable the annotation does not accept updates its default factory: call it once to validate the result against the field's annotation, then retain the callable for future defaults and reset. Failed invocation or validation preserves the previous default. A factory that produces a browser storage value is called once and its result becomes the default, so a str-annotated storage declaration keeps its storage classification and options. A type-parameter annotation checks a default against its bound or constraints and otherwise accepts any value; a protocol without @runtime_checkable accepts any value rather than raising. Mutable value defaults stay independent, a value read through a state instance is unwrapped from its proxy so the class never retains that instance, and initialized instance values stay unchanged. Reject Var and Field instances, including factory results, with guidance to use ClassVar[rx.Var] or a computed var. A shadowing ClassVar keeps ordinary assignment behavior.
Defaults no longer affect new serialization schema hashes. Deserialization also accepts compatible hashes from the previous format, which still requires the legacy declarations and defaults to match. Client-storage reset and compilation evaluate declared default factories, so factory-produced storage options reach the browser.
Addresses FINDING-004 in the October 6 prerelease results. The ComponentState example uses cls.text = initial_value and explains that reset restores the last configured default. Document callable factories and apply the requested package news wording. The reflex-base floor is 0.9.13.dev0 to require the corrected class assignment behavior.
Validation:
- Regressions failed before descriptor preservation, default validation, Field rejection, callable-factory validation, and saved-state compatibility fixes.
- Full unit suite: 11,344 passed, 159 skipped, and 73.88% coverage.
- Development and production Playwright cases pass with callable factories configured in
get_component; values render, update, reset, and remain independent across components. - Tests cover one validation call, lazy future defaults, mutable isolation, inherited defaults, failing factories, required arguments, forbidden results, validation-coroutine cleanup, accepted callables kept as values, type-parameter bounds and constraints, unchecked protocols, proxy unwrapping, factory-backed browser storage options, and
str-annotated storage factories for all three storage types. - Full repository lint, formatting, Pyright, and applicable commit hooks pass, including Ty and stub generation.
- Minimum-dependency checks pass for
reflexandreflex-base;uv lock --check --offlinepasses. - CodSpeed flags a different benchmark set on each head with a "different runtime environments" warning. Callgrind instruction counts per operation, PR vs main: metadata walk over 201 classes −3.0%,
state.handlerlookup +0.6%, scalar_isinstance−5.9%. See the PR comment for the table.
An inherited var continues to belong to its declaring state. Session-specific writes should use state instances, and shared configuration should use ClassVar.
Typing limitation: Pyright treats typed class assignments of callable factories as field values and requires a targeted ignore on those lines. Instance setter types remain strict.