Address the #7215 review follow-ups in BaseStateMeta
Merged
YA
3h ago+76+76+01234567890123456789-21-21-01234567890123456789
Summary
Closes #7264.
This handles the three follow-ups from the #7215 review that the issue lists: a dead statement in test_reserved_mixin_var, the getattr lookup of _reflex_state_root in BaseStateMeta.__new__, and the missing contract docs on BaseStateMeta.
What changed
- Test: split
test_reserved_mixin_varinto two tests. The plain-mixin case now creates the mixin outsidepytest.raises, so it shows the rejection happens when a state inherits it. The newtest_reserved_state_mixin_varasserts the rejection at the state mixin's own declaration, with no second statement left to skip. - Typed default:
BaseStateMetanow has_reflex_state_root: BaseStateMeta | None = None, and__new__reads it directly from theEvenMoreBasicBaseStatebases instead of callinggetattron every base. _is_tree_state: this also usedgetattr, withclsas the fallback. With the newNonedefault, root-less states would have counted as tree states, so it now checksis not Noneandis not cls(identity, as before). The existing suite still passed with the broken version, so I addedtest_only_states_below_a_root_are_tree_states._validate_state_name: now acceptsNoneand returns early, since a state outside any hierarchy reserves no names. Without this, pyright flags the threecls._reflex_state_rootcalls inreflex/state.py. Covered bytest_no_root_reserves_no_names.- Docstring:
BaseStateMetanow documents thattype(rx.State) is BaseStateMetais supported, thestate_root=andmixin=keywords, and the reserved_reflex_state_rootname.
Notes for reviewers
- The
Nonebranch in_validate_state_nameonly exists for the type checker. At runtime those three callers always have a root. If you'd rather narrow the type at the call sites instead, I can change it. - There's no user-facing change, so I didn't add a news fragment. Could you add the
skip-changeloglabel?
Verification
uv run pytest tests/units --cov --no-cov-on-fail --cov-report=: 13,083 passed, 81.99% coverage. Two granian reload tests intests/units/utils/test_exec.pytime out on my WSL machine (they fail onmaintoo) and pass in CI.uv run ruff check .,uv run ruff format --check .anduv run pyright reflex tests: all clean.
The code changes were made with help from Claude. I reviewed the diff and confirmed the checks above pass on my machine.