fixes #4576
This commit is contained in:
parent
3cf1fae379
commit
690d53f21b
@ -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:
|
||||
|
@ -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."""
|
||||
|
||||
|
@ -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}`."
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user