Fix rx.cond literal string typing#6545
Conversation
Greptile SummaryThis PR adds three new
Confidence Score: 3/5Runtime behavior is unchanged and the new overloads are logically sound, but several test assertions rely on Pyright narrowing an explicitly-typed Var[str] variable to Var[Literal["a"]], which is inconsistent with how Pyright handles declared-type annotations. The core overload additions look correct and the feature goal is well-motivated. The concern is in the test file: var_str is declared Var[str] but multiple assert_type calls expect Var[Literal["a", ...]]. If Pyright does not narrow through the explicit annotation, those assertions are wrong, meaning the Pyright validation step described in the PR does not actually validate what the comments claim. tests/units/components/core/test_cond.py — assertions involving var_str at lines 191, 200, 212, and 215 should be re-examined; consider re-declaring var_str as Var[Literal["a"]] or replacing those cases with direct LiteralVar.create calls. Important Files Changed
|
| # literal str, literal Var[str] -> Var[Literal[...]] | ||
| _ = assert_type(cond(True, "hello", literal_var_str), Var[Literal["hello", "a"]]) |
There was a problem hiding this comment.
the comments are confusing, literal str makes sense, but literal Var[str] when it's actually Var[LiteralString] is rather confusing
| # literal str, literal str -> Var[Literal[...]] | ||
| _ = assert_type(cond(True, "hello", "world"), Var[Literal["hello", "world"]]) |
There was a problem hiding this comment.
it would be nice to have another test for T, T, now that strings are handled differently
Summary
Preserve literal string unions in
rx.condoverloads so expressions likerx.cond(State.condition, "green", "red")type asVar[Literal["green", "red"]]instead of widening toVar[str].This fixes Pyright errors when passing conditional literal values to props typed as
Literal[...] | Var[Literal[...]], such as Radixcolor_scheme.Fixes #6538
Validation
uv run ruff format packages/reflex-components-core/src/reflex_components_core/core/cond.py tests/units/components/core/test_cond.pyuv run ruff check packages/reflex-components-core/src/reflex_components_core/core/cond.py tests/units/components/core/test_cond.pyPYRIGHT_PYTHON_FORCE_VERSION=latest uv run pyright tests/units/components/core/test_cond.py packages/reflex-components-core/src/reflex_components_core/core/cond.py --pythonversion 3.14uv run pytest tests/units/components/core/test_cond.py