bring back UntypedVarError

This commit is contained in:
Khaleel Al-Adhami 2025-02-13 13:41:20 -08:00
parent 3080cadd31
commit d1ff6d51a2
2 changed files with 13 additions and 14 deletions

View File

@ -34,8 +34,6 @@ def foreach(
TypeError: If the render function is a ComponentState.
UntypedVarError: If the iterable is of type Any without a type annotation.
"""
from reflex.state import ComponentState
iterable = LiteralVar.create(iterable).guess_type()
if isinstance(iterable, ObjectVar):
@ -50,14 +48,6 @@ def foreach(
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
)
if (
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."
)
return iterable.foreach(render_fn)

View File

@ -27,7 +27,7 @@ from typing_extensions import TypeAliasType, TypeVar
from reflex import constants
from reflex.constants.base import REFLEX_VAR_OPENING_TAG
from reflex.constants.colors import Color
from reflex.utils.exceptions import VarTypeError
from reflex.utils.exceptions import UntypedVarError, VarTypeError
from reflex.utils.types import GenericType, get_origin
from reflex.vars.base import (
CachedVarOperation,
@ -1020,8 +1020,17 @@ class ArrayVar(Var[ARRAY_VAR_TYPE], python_types=(Sequence, set)):
"Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
)
def invoke_fn(*args):
try:
return fn(*args)
except UntypedVarError as e:
raise UntypedVarError(
f"Could not foreach over var `{self!s}` without a type annotation. "
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
) from e
if num_args == 0:
fn_result = fn() # pyright: ignore [reportCallIssue]
fn_result = invoke_fn()
return_value = Var.create(fn_result)
simple_function_var: FunctionVar[ReflexCallable[[], ANOTHER_ARRAY_VAR]] = (
ArgsFunctionOperation.create(
@ -1049,7 +1058,7 @@ class ArrayVar(Var[ARRAY_VAR_TYPE], python_types=(Sequence, set)):
)
if required_num_args < 2:
fn_result = fn(first_arg) # pyright: ignore [reportCallIssue]
fn_result = invoke_fn(first_arg)
return_value = Var.create(fn_result)
@ -1072,7 +1081,7 @@ class ArrayVar(Var[ARRAY_VAR_TYPE], python_types=(Sequence, set)):
).guess_type(),
)
fn_result = fn(first_arg, second_arg) # pyright: ignore [reportCallIssue]
fn_result = invoke_fn(first_arg, second_arg)
return_value = Var.create(fn_result)