This commit is contained in:
Khaleel Al-Adhami 2025-01-29 17:00:06 -08:00
parent 3cf1fae379
commit 690d53f21b
3 changed files with 14 additions and 3 deletions

View File

@ -11,6 +11,7 @@ from reflex.components.component import Component
from reflex.components.tags import IterTag
from reflex.constants import MemoizationMode
from reflex.state import ComponentState
from reflex.utils.exceptions import UntypedVarError
from reflex.vars.base import LiteralVar, Var
@ -72,8 +73,14 @@ class Foreach(Component):
iterable=iterable,
render_fn=render_fn,
)
# Keep a ref to a rendered component to determine correct imports/hooks/styles.
component.children = [component._render().render_component()]
try:
# Keep a ref to a rendered component to determine correct imports/hooks/styles.
component.children = [component._render().render_component()]
except UntypedVarError as e:
raise UntypedVarError(
f"Could not foreach over var `{iterable!s}` without a type annotation. "
"See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
) from e
return component
def _render(self) -> IterTag:

View File

@ -75,6 +75,10 @@ class VarAttributeError(ReflexError, AttributeError):
"""Custom AttributeError for var related errors."""
class UntypedVarError(ReflexError, TypeError):
"""Custom TypeError for untyped var errors."""
class UntypedComputedVarError(ReflexError, TypeError):
"""Custom TypeError for untyped computed var errors."""

View File

@ -1216,7 +1216,7 @@ class Var(Generic[VAR_TYPE]):
raise TypeError("Cannot reverse non-list var.")
if self._var_type is Any:
raise TypeError(
raise exceptions.UntypedVarError(
f"You must provide an annotation for the state var `{self!s}`. Annotation cannot be `{self._var_type}`."
)