Skip to content

Fix rx.cond literal string typing#6545

Merged
masenf merged 4 commits into
mainfrom
fix-cond-literal-typing
Jun 23, 2026
Merged

Fix rx.cond literal string typing#6545
masenf merged 4 commits into
mainfrom
fix-cond-literal-typing

Conversation

@Alek99

@Alek99 Alek99 commented May 20, 2026

Copy link
Copy Markdown
Member

Summary

Preserve literal string unions in rx.cond overloads so expressions like rx.cond(State.condition, "green", "red") type as Var[Literal["green", "red"]] instead of widening to Var[str].

This fixes Pyright errors when passing conditional literal values to props typed as Literal[...] | Var[Literal[...]], such as Radix color_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.py
  • uv run ruff check packages/reflex-components-core/src/reflex_components_core/core/cond.py tests/units/components/core/test_cond.py
  • PYRIGHT_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.14
  • uv run pytest tests/units/components/core/test_cond.py

@codspeed-hq

codspeed-hq Bot commented May 20, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing fix-cond-literal-typing (73af84e) with main (1224c43)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Alek99 Alek99 marked this pull request as ready for review May 20, 2026 20:03
@Alek99 Alek99 requested a review from a team as a code owner May 20, 2026 20:03
@greptile-apps

greptile-apps Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds three new @overload signatures to rx.cond that use a TypeVar bound to LiteralString so expressions like rx.cond(State.flag, "green", "red") infer Var[Literal["green", "red"]] rather than widening to Var[str], eliminating Pyright errors on props typed as Literal[...] | Var[Literal[...]]. Runtime behaviour is untouched — only the type stubs change.

  • Introduces LITERAL_STRING_S = TypeVar("LITERAL_STRING_S", bound=LiteralString) and three overloads covering (literal, literal), (literal, Var[U]), and (Var[T], literal) combinations.
  • Updates the test fixture to explicitly separate literal_var_str (inferred Var[Literal["a"]]) from widened_var_str = cast(Var[str], …), addressing the type-annotation self-consistency issue raised in a prior review.

Confidence Score: 5/5

Safe 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

Filename Overview
packages/reflex-components-core/src/reflex_components_core/core/cond.py Adds three new overloads using a LiteralString-bound TypeVar to preserve literal string unions through rx.cond; runtime logic is unchanged
tests/units/components/core/test_cond.py Renames var_str to literal_var_str/widened_var_str (using cast) to distinguish Var[Literal["a"]] from Var[str] in assert_type checks, fixing the self-consistency issue flagged in a prior review
packages/reflex-components-core/news/6545.bugfix.md Changelog entry describing the literal string type preservation fix

Reviews (3): Last reviewed commit: "Address review feedback on rx.cond liter..." | Re-trigger Greptile

Comment thread packages/reflex-components-core/src/reflex_components_core/core/cond.py Outdated
Comment thread tests/units/components/core/test_cond.py
Comment thread tests/units/components/core/test_cond.py Outdated
Comment thread tests/units/components/core/test_cond.py
masenf and others added 2 commits June 23, 2026 15:48
- 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
@masenf masenf merged commit 559e79b into main Jun 23, 2026
106 checks passed
@masenf masenf deleted the fix-cond-literal-typing branch June 23, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type errors when using rx.cond on a "Literal" argument

4 participants