Validate reserved state names before registration

Merged
FA
3mo ago
+348-216

State vars named _get_was_touched or _update_was_touched can replace framework helpers and crash persistence or cleanup. Other collisions, including get_fields and dirty_vars, can break class creation or bookkeeping. This rejects reserved declarations before field collection and state initialization, and applies the same validation to dynamic vars, fields, event handlers, and route arguments.

Reserved names are derived once from BaseState and its framework bases. Ordinary vars, inherited user vars, Python protocols, and explicitly marked method overrides remain supported. Validation also checks state mixins, ordinary Python mixins, and inherited model fields without invoking descriptors.

This changes the default behavior. Existing apps can temporarily set REFLEX_STATE_ALLOW_RESERVED_NAMES=1 to retain legacy handling of conflicting vars with a deprecation warning until 1.0. The flag preserves the old behavior, including collision-related crashes; renaming conflicting declarations resolves them. Migration guidance and fragments for both affected packages are included.

Supersedes #7132 and the method-specific follow-up fix for #7135.

Closes #7091.
Closes #7135.

Validation:

  • All 35 new cases pass; on the unchanged base, the 33 regression cases fail and the two compatibility controls pass.

  • The final state suites pass all 312 tests.

  • Full unit suite: 8,626 passed, 18 skipped. The coverage gate exits nonzero at 71.34%, below the required 72%; the unchanged base also reports 71.34% with 8,591 passed and 18 skipped. The new validator has 97.59% coverage; state plus validation have 86.19%.

  • Full pyright reflex tests passes. All commit hooks pass, including Ruff, codespell, stub generation, Pyright, and ty.

  • Repository Ruff checks and formatting pass with the ignored ignore/ scratch tree excluded.

  • Followed CONTRIBUTING.md and added regression tests, migration documentation, and package news fragments.

  • Breaking change with a temporary compatibility opt-in.

  • Checked overlapping work: this is the centralized replacement for #7132 and the narrow #7135 follow-up.