ENG-10096: Make Field, ComputedVar and EventHandler state descriptors

Merged
MA
2mo ago
+2133-1690

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)
  • This change requires a documentation update

Part 1 of 4 of #7302. Stack: 1 field descriptors → 2 state locks → 3 core state in reflex-base → 4 dispatch_value.

Description

Replaces BaseState._get_attribute and the __setattr__ routing with real descriptors:

  • Field is a data descriptor bound to the class that declares it. It stores the value in the owning state's __dict__, fills in defaults lazily, type-checks, wraps mutable values in MutableProxy and marks itself dirty. ComputedVar and EventHandler also resolve through descriptors.
  • Inheritance works like plain Python: an inherited var resolves to the ancestor state that owns it. Redeclaring a var in a substate makes an independent var, and the var-vs-var shadowing errors are gone. The reserved-name check stays.
  • A write invalidates only the computed vars that depend on the changed var, instead of recomputing all of them.
  • Bookkeeping (parent_state, substates, dirty_vars and the like) moves into __slots__, out of the pickled dict. Pickles from the previous release still load. Pickles written now also carry compatibility keys, so workers still on the previous release can load them during a rolling deploy.
  • Delta building moves to reflex/istate/delta.py, and get_delta only looks at fields.
  • The undeclared-attribute typo guard runs only outside prod mode.

In prod mode, state var reads go from about 1050 ns to about 250 ns, and writes from about 9.8 µs to about 660 ns (tests/benchmarks/test_state_access.py).

Breaking: internals like _get_attribute, get_skip_vars, backend_vars, inherited_vars, _dirty_computed_vars, is_backend_base_variable and RESERVED_BACKEND_VAR_NAMES are removed. The news fragments list the rest.

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?

🤖 Generated with Claude Code

https://claude.ai/code/session_0145YmAVjKSJhdUFDcBP4FB9


Generated by Claude Code