feat(state): let delta coroutines drop their key via sentinel#6673
feat(state): let delta coroutines drop their key via sentinel#6673FarhanAliRaza wants to merge 3 commits into
Conversation
A coroutine value in a delta can now resolve to _DROP_FROM_DELTA to omit its key, so inclusion that can only be decided asynchronously is deferred into the delta and resolved post-hoc by _resolve_delta. Emptied state subdicts are removed as well.
Merging this PR will not alter performance
Comparing Footnotes
|
Greptile SummaryThis PR introduces a
Confidence Score: 5/5Safe to merge; the sentinel drop logic is correct and the EventContext rebinding is properly scoped and restored. Both changes are narrowly scoped: _resolve_delta only gains a sentinel identity-check with an empty-subdict cleanup, and modify_state's ExitStack pattern correctly restores context on both normal and exceptional exits. reflex/state.py — Final should be added to the typing import block. Important Files Changed
Reviews (3): Last reviewed commit: "Update reflex/state.py" | Re-trigger Greptile |
| return value | ||
|
|
||
| delta = {"s1": {"gone": _coro(_DROP_FROM_DELTA), "stay": _coro("kept"), "plain": 3}} | ||
| resolved = await _resolve_delta(delta) | ||
| assert resolved == {"s1": {"stay": "kept", "plain": 3}} | ||
|
|
||
|
|
||
| async def test_resolve_delta_pops_subdict_emptied_by_drops(): | ||
| """A state subdict left empty after dropping all its keys is removed entirely.""" | ||
| from reflex.state import _DROP_FROM_DELTA, _resolve_delta | ||
|
|
||
| async def _coro(value): # noqa: RUF029 - a trivial coroutine value for the delta | ||
| return value | ||
|
|
||
| delta = {"s1": {"only": _coro(_DROP_FROM_DELTA)}, "s2": {"keep": 1}} | ||
| resolved = await _resolve_delta(delta) | ||
| assert resolved == {"s2": {"keep": 1}} |
There was a problem hiding this comment.
Missing multi-drop-in-same-subdict test
The existing subdict-emptying test (test_resolve_delta_pops_subdict_emptied_by_drops) only exercises the case where a single coroutine empties the subdict. The more interesting ordering-sensitive path — where two coroutines in the same subdict both resolve to _DROP_FROM_DELTA — isn't covered. The empty-check logic (if not delta[state_name]) relies on the second coroutine's raw coroutine object still being in the dict when the first drop's check runs; while the logic is correct, adding a test case like {"s1": {"a": _coro(_DROP_FROM_DELTA), "b": _coro(_DROP_FROM_DELTA)}} would pin this invariant explicitly.
Out-of-band modify_state (e.g. the shared-state fan-out recomputing another client's delta) runs in a task that copied the triggering event's EventContext. Fork and rebind the context to the modified token so consumers inside -- delta resolution, computed vars -- observe that token rather than the actor's inherited one. No-op when no EventContext is set.
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
A coroutine value in a delta can now resolve to _DROP_FROM_DELTA to omit its key, so inclusion that can only be decided asynchronously is deferred into the delta and resolved post-hoc by _resolve_delta. Emptied state subdicts are removed as well.
All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features: