some random fixes after merge

This commit is contained in:
Khaleel Al-Adhami 2025-02-13 13:36:45 -08:00
parent a2074b9081
commit 3080cadd31
7 changed files with 52 additions and 64 deletions

View File

@ -41,7 +41,7 @@ class ClientSideRouting(Component):
return "" return ""
def wait_for_client_redirect(component: Component) -> Component: def wait_for_client_redirect(component: Component) -> Var[Component]:
"""Wait for a redirect to occur before rendering a component. """Wait for a redirect to occur before rendering a component.
This prevents the 404 page from flashing while the redirect is happening. This prevents the 404 page from flashing while the redirect is happening.
@ -53,9 +53,9 @@ def wait_for_client_redirect(component: Component) -> Component:
The conditionally rendered component. The conditionally rendered component.
""" """
return cond( return cond(
condition=route_not_found, route_not_found,
c1=component, component,
c2=ClientSideRouting.create(), ClientSideRouting.create(),
) )

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Any, Union, overload from typing import Any, TypeVar, Union, overload
from reflex.components.base.fragment import Fragment from reflex.components.base.fragment import Fragment
from reflex.components.component import BaseComponent, Component from reflex.components.component import BaseComponent, Component
@ -13,14 +13,20 @@ from reflex.vars.number import ternary_operation
@overload @overload
def cond(condition: Any, c1: Component, c2: Any = None) -> Component: ... def cond(
condition: Any, c1: BaseComponent | Var[BaseComponent], c2: Any = None, /
) -> Var[Component]: ...
T = TypeVar("T")
V = TypeVar("V")
@overload @overload
def cond(condition: Any, c1: Any, c2: Any) -> Var: ... def cond(condition: Any, c1: T | Var[T], c2: V | Var[V], /) -> Var[T | V]: ...
def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var: def cond(condition: Any, c1: Any, c2: Any = None, /) -> Var:
"""Create a conditional component or Prop. """Create a conditional component or Prop.
Args: Args:
@ -59,14 +65,17 @@ def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var:
@overload @overload
def color_mode_cond(light: Component, dark: Component | None = None) -> Component: ... # pyright: ignore [reportOverlappingOverload] def color_mode_cond(
light: BaseComponent | Var[BaseComponent],
dark: BaseComponent | Var[BaseComponent] | None = ...,
) -> Var[Component]: ...
@overload @overload
def color_mode_cond(light: Any, dark: Any = None) -> Var: ... def color_mode_cond(light: T | Var[T], dark: V | Var[V]) -> Var[T | V]: ...
def color_mode_cond(light: Any, dark: Any = None) -> Var | Component: def color_mode_cond(light: Any, dark: Any = None) -> Var:
"""Create a component or Prop based on color_mode. """Create a component or Prop based on color_mode.
Args: Args:

View File

@ -4,9 +4,8 @@ from __future__ import annotations
from typing import Callable, Iterable from typing import Callable, Iterable
from reflex.vars import ArrayVar, ObjectVar, StringVar
from reflex.vars.base import LiteralVar, Var from reflex.vars.base import LiteralVar, Var
from reflex.vars.object import ObjectVar
from reflex.vars.sequence import ArrayVar
class ForeachVarError(TypeError): class ForeachVarError(TypeError):
@ -30,44 +29,37 @@ def foreach(
Returns: Returns:
The foreach component. The foreach component.
Raises: Raises:
ForeachVarError: If the iterable is of type Any. ForeachVarError: If the iterable is of type Any.
TypeError: If the render function is a ComponentState. TypeError: If the render function is a ComponentState.
UntypedVarError: If the iterable is of type Any without a type annotation. UntypedVarError: If the iterable is of type Any without a type annotation.
""" """
from reflex.vars import ArrayVar, ObjectVar, StringVar from reflex.state import ComponentState
iterable = LiteralVar.create(iterable).guess_type() iterable = LiteralVar.create(iterable).guess_type()
if iterable._var_type == Any: if isinstance(iterable, ObjectVar):
raise ForeachVarError( iterable = iterable.entries()
f"Could not foreach over var `{iterable!s}` of type Any. "
"(If you are trying to foreach over a state var, add a type annotation to the var). "
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
)
if ( if isinstance(iterable, StringVar):
hasattr(render_fn, "__qualname__") iterable = iterable.split()
and render_fn.__qualname__ == ComponentState.create.__qualname__
):
raise TypeError(
"Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
)
if isinstance(iterable, ObjectVar): if not isinstance(iterable, ArrayVar):
iterable = iterable.entries() raise ForeachVarError(
f"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. "
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
)
if isinstance(iterable, StringVar): if (
iterable = iterable.split() hasattr(render_fn, "__qualname__")
and render_fn.__qualname__ == ComponentState.create.__qualname__
):
raise TypeError(
"Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
)
if not isinstance(iterable, ArrayVar): return iterable.foreach(render_fn)
raise ForeachVarError(
f"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. "
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
)
return iterable.foreach(render_fn)
class Foreach: class Foreach:
"""Create a foreach component.""" """Create a foreach component."""

View File

@ -2403,14 +2403,7 @@ class ComputedVar(Var[RETURN_TYPE]):
self: ComputedVar[SEQUENCE_TYPE], self: ComputedVar[SEQUENCE_TYPE],
instance: None, instance: None,
owner: Type, owner: Type,
) -> ArrayVar[list[LIST_INSIDE]]: ... ) -> ArrayVar[SEQUENCE_TYPE]: ...
@overload
def __get__(
self: ComputedVar[tuple[LIST_INSIDE, ...]],
instance: None,
owner: Type,
) -> ArrayVar[tuple[LIST_INSIDE, ...]]: ...
@overload @overload
def __get__(self, instance: None, owner: Type) -> ComputedVar[RETURN_TYPE]: ... def __get__(self, instance: None, owner: Type) -> ComputedVar[RETURN_TYPE]: ...
@ -2646,17 +2639,10 @@ class AsyncComputedVar(ComputedVar[RETURN_TYPE]):
@overload @overload
def __get__( def __get__(
self: AsyncComputedVar[list[LIST_INSIDE]], self: AsyncComputedVar[SEQUENCE_TYPE],
instance: None, instance: None,
owner: Type, owner: Type,
) -> ArrayVar[list[LIST_INSIDE]]: ... ) -> ArrayVar[SEQUENCE_TYPE]: ...
@overload
def __get__(
self: AsyncComputedVar[tuple[LIST_INSIDE, ...]],
instance: None,
owner: Type,
) -> ArrayVar[tuple[LIST_INSIDE, ...]]: ...
@overload @overload
def __get__( def __get__(

View File

@ -1880,11 +1880,11 @@ def _generate_overloads_for_function_var_call(maximum_args: int = 4) -> str:
): ):
for return_type, return_type_var in return_type_mapping.items(): for return_type, return_type_var in return_type_mapping.items():
required_args = [ required_args = [
f"arg{j + 1}: Union[V" f"{j + 1}, Var[V{j + 1}]]" f"arg{j + 1}: Union[V{j + 1}, Var[V{j + 1}]]"
for j in range(number_of_required_args) for j in range(number_of_required_args)
] ]
optional_args = [ optional_args = [
f"arg{j + 1}: Union[V" f"{j + 1}, Var[V{j + 1}], Unset] = Unset()" f"arg{j + 1}: Union[V{j + 1}, Var[V{j + 1}], Unset] = Unset()"
for j in range( for j in range(
number_of_required_args, number_of_required_args,
number_of_required_args + number_of_optional_args, number_of_required_args + number_of_optional_args,

View File

@ -28,6 +28,7 @@ from reflex.utils.types import (
get_attribute_access_type, get_attribute_access_type,
get_origin, get_origin,
safe_issubclass, safe_issubclass,
unionize,
) )
from .base import ( from .base import (

View File

@ -473,7 +473,7 @@ def array_length_operation(array: Var[ARRAY_VAR_TYPE]):
@var_operation @var_operation
def string_split_operation( def string_split_operation(
string: Var[str], sep: VarWithDefault[str] = VarWithDefault("") string: Var[STRING_TYPE], sep: VarWithDefault[STRING_TYPE] = VarWithDefault("")
): ):
"""Split a string. """Split a string.