Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from reflex_base.vars import VarData
from reflex_base.vars.base import LiteralVar, Var
from reflex_base.vars.number import ternary_operation
from typing_extensions import LiteralString

from reflex_components_core.base.bare import Bare
from reflex_components_core.base.fragment import Fragment
Expand Down Expand Up @@ -131,6 +132,19 @@ def cond(condition: Any, c1: Any, c2: Component, /) -> Component: ... # pyright

T = TypeVar("T", covariant=True)
U = TypeVar("U", covariant=True)
S = TypeVar("S", bound=LiteralString)
Comment thread
Alek99 marked this conversation as resolved.
Outdated


@overload
def cond(condition: Any, c1: S, c2: S, /) -> Var[S]: ... # pyright: ignore [reportOverlappingOverload]


@overload
def cond(condition: Any, c1: S, c2: Var[U], /) -> Var[S | U]: ... # pyright: ignore [reportOverlappingOverload]


@overload
def cond(condition: Any, c1: Var[T], c2: S, /) -> Var[T | S]: ... # pyright: ignore [reportOverlappingOverload]


@overload
Expand Down
28 changes: 20 additions & 8 deletions tests/units/components/core/test_cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,32 @@ def test_cond_assert_types() -> None:
# non-component, Component -> Component
_ = assert_type(cond(True, "hello", text_comp), Component)

# T, T -> Var[T]
_ = assert_type(cond(True, "hello", "world"), Var[str])
# literal str, literal str -> Var[Literal[...]]
_ = assert_type(cond(True, "hello", "world"), Var[Literal["hello", "world"]])
Comment on lines +185 to +186
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have another test for T, T, now that strings are handled differently


# T, U -> Var[T | U]
_ = assert_type(cond(True, "hello", 3), Var[str | int])

# T, Var[T] -> Var[T]
_ = assert_type(cond(True, "hello", var_str), Var[str])
# literal str, literal Var[str] -> Var[Literal[...]]
_ = assert_type(cond(True, "hello", var_str), Var[Literal["hello", "a"]])

# Var[T], T -> Var[T]
_ = assert_type(cond(True, var_str, "world"), Var[str])
# literal str, literal Var[str] -> Var[Literal[...]]
_ = assert_type(
cond(True, "hello", LiteralVar.create("world")),
Var[Literal["hello", "world"]],
)

# literal Var[str], literal str -> Var[Literal[...]]
_ = assert_type(cond(True, var_str, "world"), Var[Literal["a", "world"]])

# literal Var[str], literal str -> Var[Literal[...]]
_ = assert_type(
cond(True, LiteralVar.create("hello"), "world"),
Var[Literal["hello", "world"]],
)

# T, Var[U] -> Var[T | U]
_ = assert_type(cond(True, "hello", var_int), Var[str | int])
# literal str, Var[U] -> Var[Literal[...] | U]
_ = assert_type(cond(True, "hello", var_int), Var[int | Literal["hello"]])
Comment thread
Alek99 marked this conversation as resolved.

# Var[T], U -> Var[T | U]
_ = assert_type(cond(True, var_str, 3), Var[int | Literal["a"]])
Expand Down
Loading