Fix class-level State attribute regressions from #7461 / #7465 (patch restore, storage defaults, mangled-name guard)

Merged
MA
24d ago
+892-19

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

  • Bug fix (non-breaking change which fixes an issue)

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

Three commits, one per issue, all in the class-level State attribute handling introduced by #7461 and #7465. Found by the 0.10.0a2 pre-release QA (prerelease_testing/2026-10-07/FINDINGS.md on claude/reflex-prerelease-testing-t0sd90, findings N-039, N-005, N-008; the first was independently verified across 15 var kinds Ɨ 7 patch mechanisms).

  1. Restore patched state var defaults when a patch is undone (fixes #7472). BaseStateMeta.__setattr__ mutated the one shared Field in place, so the attribute monkeypatch.setattr / mock.patch.object save from cls.__dict__ was that same mutated Field, and assigning it back hit _accepts_default's Field/Var rejection (TypeError: A Field cannot overwrite another field); with no __delattr__, undoing a patch made through a substate raised AttributeError, and the patched default leaked into later tests. Now a class assignment records the default it replaces on the field (Field._assign_default / _restore_default), assigning a var's own Field or its public Var back undoes the most recent assignment, and a new BaseStateMeta.__delattr__ does the same while keeping the descriptor. Field identity is preserved, Fields/Vars of other vars are still rejected, plain values still update the default.
  2. Keep browser storage when assigning a plain default to a storage var (fixes #7471). A storage var is recognised only through its default, so State.pref = "dark" (including the documented ComponentState cls.pref = initial) replaced the LocalStorage/Cookie/SessionStorage default with a bare str and silently dropped persistence. ClientStorageBase._with_value rebuilds the same storage type with the same options and _keep_client_storage applies it to an assigned plain string and to the result of an assigned factory; assigning a new storage instance still replaces it.
  3. Reject undeclared names that only look name-mangled in dev mode (fixes #7473). The guard accepted name.startswith("_") and "__" in name, so self._x__y = 1 on an undeclared name was silently accepted. _is_plain_private_name accepts names starting with __ and names mangled by any class in the state's MRO (using __original_name__ for renamed local classes, stripping leading underscores as Python does).

Test plan

  • tests/units/reflex_base/vars/test_base.py::test_class_assignment_patch_round_trip (10 var kinds Ɨ monkeypatch, mock.patch.object, manual getattr/restore, delattr) plus nested-patch and delattr tests, and tests/units/components/test_component_state.py::test_component_state_patch_round_trip: 43 failures before, all pass after. The QA verifier's test_min_t1.py / test_t1_pytest.py run against this tree: 29/29 (on 0.10.0a2: 8 failed, 6 errors).
  • test_plain_default_assignment_keeps_browser_storage (3 storage types Ɨ str or storage annotation Ɨ value or factory; 12/12 failed before) and test_component_state_storage_default_keeps_browser_storage; Playwright HydrationStorageApp gains a page with class-assigned LocalStorage/Cookie defaults and a ComponentState cls.pref = initial, and test_assigned_storage_default_persists checks a new session reads them from the browser (times out before the fix; passes in dev and prod after).
  • tests/units/test_state.py::test_undeclared_double_underscore_name_is_rejected covers own, mixin, renamed-local and base mangling plus __marker__; _x__y, _sneaky__name and look-alike prefixes raise (they did not before); the #7465 tests still pass.
  • ruff check / ruff format clean, pyright reflex tests 0 errors, Playwright -k storage 6/6 dev and prod; the full unit suite's only failures are the 232 reflex_cli/v2 version-check tests that fail identically on main in a shallow checkout. Docs (docs/vars/base_vars.md) and news fragments in the root and reflex-base.

Review notes: restore undoes one assignment step rather than jumping to the class-body default, so a default configured before the patch (cls.count = 5 in get_component) comes back and nested patches unwind in order; State.x = State.x used to raise and is now a silent undo; the per-field undo list grows by one entry per class assignment that is never undone. The guard keeps 0.9.12's exemption for all __-prefixed names (__cached_* computed-var caches rely on it) rather than only __x__.

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_01SjCrbdguMkfe4bhbaREiwo


Generated by Claude Code