Repository navigation
feat(vars): add SetVar for set and frozenset vars - #7642
FarhanAliRaza wants to merge 5 commits into
Conversation
A set-typed state var or literal was an ArrayVar, so it exposed sequence operations (indexing, map, slicing) that a set does not have, and a frozenset was rejected by LiteralVar.create outright. Add SetVar and LiteralSetVar for set[T] and frozenset[T]. The wire value stays the JSON array that serialize_set already produces, which also round-trips through event arguments, so no frontend rehydration is needed: each operation compiles to JavaScript over that array, the way DateTimeVar compiles over a string. contains, length, union, intersection, difference, symmetric_difference, issubset, issuperset and isdisjoint return SetVar or BooleanVar with the element type carried through; to_array is the JS identity typed as a Sequence, and rx.foreach calls it. Remove set from ArrayVar's python types and route set and frozenset to SetVar in figure_out_type, Var.to, var_operation and the Field and ComputedVar overloads. The sequence operations still work on a set var for one release with a deprecation warning pointing at to_array(). A set literal renders its elements sorted when they compare, so the compiled output no longer depends on the process hash seed. Closes #7623 Claude-Session: https://claude.ai/code/session_01FTaqSdpd95XTrwgxAHExRd
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4fc60c7bda
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
[Medium impact] The PR appears safe to merge; no new actionable issues remain. SummaryAdds
Reviews (4) · Last reviewed commit: "docs: record the set var array-operation..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 14 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will not alter performance1 warning
Comparing Footnotes |
Address review findings on the SetVar change: - Move the set operations into pySet* helpers in state.js that key elements by value, so a set of tuples (nested arrays on the frontend) gets Python answers from contains, union, intersection and the rest instead of array-identity comparison. - Register a serializer for frozenset, so a frozenset state field or computed var is valid and reaches the frontend as an array. - Keep an optional set optional in to_array(), so rx.foreach still applies its null guard. - Let the deprecated `set + set` and `array + set` fallbacks compile by viewing set operands as arrays. - Set the deprecation version to 0.10.1, the next release after v0.10.0. - Check VarData propagation through a set literal in the tests. Claude-Session: https://claude.ai/code/session_01FTaqSdpd95XTrwgxAHExRd
symmetric_difference with a plain array operand kept that array's duplicates; build it as a union so the result is a set. Key strings and objects with different prefixes in pySetKey, so a string can never share a key with a tuple's JSON. Claude-Session: https://claude.ai/code/session_01FTaqSdpd95XTrwgxAHExRd
|
marking as a [breaking] change, because before you could index into |
|
Agreed. 7374337 moves the changelog fragment for this to the Generated by Claude Code |
Closes #7623
Type of change
Changes To Core Features
What changed
A
set-typed state var or literal was anArrayVar, so it exposed sequence operations a set does not have, andfrozensetwas rejected byLiteralVar.create. #7622 widenedArrayVarinstead and was reverted in #7632.This adds
SetVar/LiteralSetVarinreflex_base/vars/set.pyforset[T]andfrozenset[T], following theDateTimeVarpattern: a typed var over an untyped wire value.serialize_setis unchanged (andfrozensetgains the same serializer, so it is now a valid state var type) and nothing inapplyDeltachanges. An array also round-trips through event arguments, which a real JSSetwould not (JSON.stringify(new Set(["a"]))is"{}"). The issue's "rehydrate asSeton the frontend" prerequisite is dropped for that reason.pySet*helpers instate.js(next topyFlatMap) that key elements by value, so aset[tuple[int, int]]gets Python answers rather than array-identity comparison:contains/has,length/size,union,intersection,difference,symmetric_difference,issubset,issuperset,isdisjoint, andto_array()which is the JS identity typed asSequence[T](optional sets stay optional). Set-returning operations keepfrozenset-ness and carry the element type through.setis removed fromArrayVar.python_types;figure_out_type,Var.to,var_operation,Field.__get__andComputedVar.__get__routeset/frozensettoSetVar, soState.tags.contains("x")is aBooleanVarstatically with no cast.rx.foreachaccepts aSetVarby callingto_array().map,filter,join,reverse,pluck,reduce,flat_map,+(either side) and*still work on a set var for one release with aconsole.deprecatepointing at.to_array(), and are marked@deprecatedfor type checkers.LiteralArrayVar's element var-data and JSON helpers are extracted soLiteralSetVarshares them.Tests
tests/units/reflex_base/vars/test_set.py: literal creation and static types (typing.assert_typeunder pyright), the rendered JS and helper import of every operation, tuple-element sets, operand validation,to_array(including optional sets), the deprecated fallbacks and concatenation in both directions, state fields (set,frozenset, optional) and computed vars,rx.foreach, VarData propagation.tests/integration/tests_playwright/test_set_operations.py: backendadd/discardre-render every operation and aforeach; afrozensetfield; a set of tuples compared by value; an unset optional set renders no children; a set is sent back through an event argument and received intact (as a list, which is existing behavior for event args).uv run ruff check .,uv run ruff format .,uv run pyright reflex tests packages/reflex-base/src/reflex_base/vars packages/reflex-components-core/srcare clean. The unit suite passes apart fromtests/units/reflex_cli/v2, which fails identically onmainin this environment (the hosting CLI rejects the0.0.1.devversion string).Docs and example apps checked for breakage
docs/enterprise/mantine/pill.mdusestags: set[str]withrx.foreachonly: still works throughto_array().docs/guides/ai_applications.mdusesfrozensetin a backend dataclass only: unaffected.docs/vars/var-operations.mdgains a "Set Operations" section anddocs/api-reference/var_system.mdlistsSetVar.Out of scope, as the issue says
Sequence[...]-typed prop (typehint_issubclass) is unchanged.https://claude.ai/code/session_01FTaqSdpd95XTrwgxAHExRd