Fix class-level State attribute regressions from #7461 / #7465 (patch restore, storage defaults, mangled-name guard)
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).
- Restore patched state var defaults when a patch is undone (fixes #7472).
BaseStateMeta.__setattr__mutated the one sharedFieldin place, so the attributemonkeypatch.setattr/mock.patch.objectsave fromcls.__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 raisedAttributeError, 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 newBaseStateMeta.__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. - 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 ComponentStatecls.pref = initial) replaced theLocalStorage/Cookie/SessionStoragedefault with a barestrand silently dropped persistence.ClientStorageBase._with_valuerebuilds the same storage type with the same options and_keep_client_storageapplies it to an assigned plain string and to the result of an assigned factory; assigning a new storage instance still replaces it. - Reject undeclared names that only look name-mangled in dev mode (fixes #7473). The guard accepted
name.startswith("_") and "__" in name, soself._x__y = 1on an undeclared name was silently accepted._is_plain_private_nameaccepts 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, andtests/units/components/test_component_state.py::test_component_state_patch_round_trip: 43 failures before, all pass after. The QA verifier'stest_min_t1.py/test_t1_pytest.pyrun 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) andtest_component_state_storage_default_keeps_browser_storage; PlaywrightHydrationStorageAppgains a page with class-assigned LocalStorage/Cookie defaults and a ComponentStatecls.pref = initial, andtest_assigned_storage_default_persistschecks 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_rejectedcovers own, mixin, renamed-local and base mangling plus__marker__;_x__y,_sneaky__nameand look-alike prefixes raise (they did not before); the #7465 tests still pass.ruff check/ruff formatclean,pyright reflex tests0 errors, Playwright-k storage6/6 dev and prod; the full unit suite's only failures are the 232reflex_cli/v2version-check tests that fail identically onmainin 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