Keep states defined in test_state tests out of later tests

BE
8h ago
+27-43

Summary

Some tests in test_state.py leave registrations behind that break later tests, depending on the order the tests run in. CI runs the unit tests in default order, where test_app.py comes before test_state.py, so it doesn't hit this. Running a selection with -k, --lf, or the files in another order does.

  • test_computed_var_mutability defines states with a deliberately invalid deps=["hi"] and never removes them. Every later App compile then fails dependency validation. pytest tests/units/test_state.py tests/units/test_app.py -p no:randomly fails 17 test_app tests on main.
  • The router-dependency tests and test_composite_var_dep_tracks_fields_in_every_state register their local states in the class-level dependency maps of State and other module-level states. They remove those entries at the end of the test body, so a test that fails leaves them behind, and the next test that dirties one of those vars looks up a substate that no longer exists. Under random order, test_state.py alone fails 7 of 8 seeds (--randomly-seed=1..8).
  • The two redis config-knob tests call reload_config() with a redis://localhost config, which stays on the root registration context. Later tests then create a redis state manager and try to connect.

forked_registration_context already gives a test its own copy of the registrations and the config. It now also snapshots the dependency tracking (_var_dependencies, _potentially_dirty_states, _always_dirty_substates) of the states registered before the fork, and restores it on teardown. That covers the entries a state defined in the fork adds to classes that outlive it. The tests above use the fixture, and the manual cleanup at the end of their bodies is gone, so teardown runs even when an assertion fails.

Test-only change, no news fragment.

Test plan

  • pytest tests/units/test_state.py tests/units/test_app.py -p no:randomly: 17 failed before, 670 passed after
  • pytest tests/units/test_state.py --randomly-seed=N for N = 1..10: 336 passed for every seed (before: failures on 7 of seeds 1..8)
  • pytest tests/units/test_state.py tests/units/test_app.py --randomly-seed=N for N = 1..4: 670 passed
  • uv run pytest tests/units: 16458 passed
  • ruff check, ruff format --check, pyright

🤖 Generated with Claude Code