Fix rx.cond literal string typing#6545
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Greptile SummaryAdds three new
Confidence Score: 5/5Safe to merge — changes are purely additive type-checker stubs with no runtime impact. The three new overloads correctly use a LiteralString-bound TypeVar to capture literal string types without affecting the implementation body. The test updates properly distinguish Var[Literal["a"]] from Var[str] using cast, resolving the self-consistency issue flagged in a prior review. No logic paths, serialization, or runtime behaviour is altered. No files require special attention. Important Files Changed
Reviews (3): Last reviewed commit: "Address review feedback on rx.cond liter..." | Re-trigger Greptile |
- Clarify the confusing `literal Var[str]` test comments to `Var[Literal[...]]`, matching the actual inferred type of the literal var. - Add an assert_type case for the generic non-string `T, T -> Var[T]` path, now that literal strings take dedicated narrowing overloads. - Add a reflex-components-core changelog fragment for the fix. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XnVNErQeUE5xQZhXLySzkp
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