Back out class-level default assignment; refuse to assign over a state var

Merged
MA
24d ago
+303-1353

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__ raises TypeError when the name resolves, through the MRO, to a state var's Field, including a var inherited by a substate. Other names are unaffected: new names, ClassVars (including one redeclaring an inherited var), handlers and setters. A name 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.object quiet.
  • del State.x still removes the descriptor. Deleting first, then setting, replaces the var on purpose.
  • add_var/add_field bind through the guard, because add_var rejects any name already in __fields__. _bind_attr keeps the type.__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_default and the replaced-default history.
  • _accepts_default, _keep_client_storage, _assigned_field, Field._client_storage and ClientStorageBase._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() using default_value();
    • storage-annotated vars declared with a default_factory compile 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.object on 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 /assigned page.
  • 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 236 reflex_cli/v2 and reflex_bench browser tests that fail identically on main here;
    • Playwright test_hydration_storage.py and test_component_state.py pass in dev and prod (12/12);
    • Selenium test_client_storage.py passes. test_component_state.py::test_component_state_app fails 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.

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