Back out class-level default assignment; refuse to assign over a state var
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
- Breaking change (fix or feature that would cause existing functionality to not work as expected)
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?
What and why
#7461 made State.var = value update the var's default instead of replacing its Field descriptor. #7495 then added patch/restore history and kept browser storage on plain assignments, and #7512 was fixing the remaining patch edge cases. Per the review on #7512, that layer on top of the Field descriptor is not ready for 0.10, so it comes out of the release.
Reverting #7461 outright would bring back the 0.10.0a1 behavior: assignment replaced the descriptor, which broke the var (a backend var read the value only until pickled, then None; dev writes and reset() raised). Instead, the metaclass now refuses an assignment over a state var:
State._x = 5
# TypeError: '_x' is a state var of State; assigning it on the class would replace
# the var. Set its default with State.__fields__['_x'].default = ..., or declare
# class-level config as ClassVar.
BaseStateMeta.__setattr__raisesTypeErrorwhen the name resolves, through the MRO, to a state var'sField, including a var inherited by a substate. Other names are unaffected: new names, ClassVars (including one redeclaring an inherited var), handlers and setters. Aname in cls.__fields__check skips the MRO walk for them.- Assigning a var's own field back is a no-op. That keeps the cleanup of a failed
mock.patch.objectquiet. del State.xstill removes the descriptor. Deleting first, then setting, replaces the var on purpose.add_var/add_fieldbind through the guard, becauseadd_varrejects any name already in__fields__._bind_attrkeeps thetype.__setattr__it had since #7461: a field copied from a mixin replaces the name that resolves to the mixin's field.
Removed:
Field._assign_default/_restore_default/_keep_defaultand the replaced-default history._accepts_default,_keep_client_storage,_assigned_field,Field._client_storageandClientStorageBase._with_value.- The metaclass
__delattr__. - Their tests and news fragments.
Kept (independent of the assignment layer):
- From #7461:
- the saved-state schema without defaults, with its legacy fallback;
reset()usingdefault_value();- storage-annotated vars declared with a
default_factorycompile with the options the factory produces; - TypeVar/Protocol handling in
_isinstance.
- From #7495: the dev-mode guard against undeclared mangled names.
Setting defaults now goes through the field. The ComponentState docs go back to the pre-#7461 pattern:
cls.__fields__["text"].default = initial_value
cls.__fields__["items"].default_factory = lambda: [label]
cls.__fields__["theme"].default = rx.LocalStorage(initial, name=f"theme_{key}")
Each generated ComponentState class owns its field copies (the mixin copy, independent of #7461), so this is per component. A browser storage var stays in storage only with a storage value as its default. The docs now say so, and fold in #7514's note about the key shared by a named storage var.
Tests patch the field:
with mock.patch.object(State.__fields__["_limit"], "default", 99):
...
monkeypatch.setattr(State, "_limit", v) and mock.patch.object(State, "_limit", v) assign straight over the attribute, so they raise now. monkeypatch.delattr(State, "_limit") followed by monkeypatch.setattr(State, "_limit", v, raising=False) round-trips.
Compatibility
0.9.12 accepted State.var = value, but new instances still started at the declared default. On 0.10 the assignment raises. The upgrade guide's section and the breaking fragments give the migration: set the default on the field, patch the field in tests, and use ClassVar for class-level configuration. A ClassVar behaves as on 0.9.
Tests
- New unit tests (
tests/units/reflex_base/vars/test_base.py):- assignment over a frontend or backend var raises on the declaring state and on a substate, and leaves the field in place;
- ClassVars, including one over an inherited var, and new names stay assignable;
- patching the field's default round-trips, and so does
monkeypatch.delattr+setattr; mock.patch.objecton the class attribute raises, and its cleanup leaves the var intact.
- Converted to
__fields__:- the per-component default test (now also checks a per-component storage key);
- the schema test;
- the integration ComponentState app;
- the hydration-storage
/assignedpage.
- Removed: the class-assignment, patch-history and storage-collapse tests.
- Results:
- the unit suite matches
main's results in this sandbox: the only failures are the same 236reflex_cli/v2andreflex_benchbrowser tests that fail identically onmainhere; - Playwright
test_hydration_storage.pyandtest_component_state.pypass in dev and prod (12/12); - Selenium
test_client_storage.pypasses.test_component_state.py::test_component_state_appfails here, but it fails the same way on the pre-change code, so it's an environment issue; pyright reflex tests: 0 errors;- pre-commit clean.
- the unit suite matches
Supersedes #7512 (patch-undo fixes for the removed layer) and #7514 (docs for the assignment pattern).
🤖 Generated with Claude Code
https://claude.ai/code/session_01SjCrbdguMkfe4bhbaREiwo
Generated by Claude Code