Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a Var is Optional, iterate over it as if it is not #4511

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion reflex/components/core/foreach.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def render(self):
iterable_state=str(tag.iterable),
arg_name=tag.arg_var_name,
arg_index=tag.get_index_var_arg(),
iterable_type=tag.iterable._var_type.mro()[0].__name__,
iterable_type=tag.get_iterable_var_type().__name__,
)


Expand Down
14 changes: 9 additions & 5 deletions reflex/components/tags/iter_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, Union, get_args

from reflex.components.tags.tag import Tag
from reflex.utils.types import is_optional
from reflex.vars import LiteralArrayVar, Var, get_unique_variable_name

if TYPE_CHECKING:
Expand Down Expand Up @@ -38,15 +39,18 @@ def get_iterable_var_type(self) -> Type:
The type of the iterable var.
"""
iterable = self.iterable
var_type = iterable._var_type
if is_optional(var_type):
var_type = get_args(var_type)[0]
try:
if iterable._var_type.mro()[0] is dict:
if var_type.mro()[0] is dict:
# Arg is a tuple of (key, value).
return Tuple[get_args(iterable._var_type)] # type: ignore
elif iterable._var_type.mro()[0] is tuple:
return Tuple[get_args(var_type)] # type: ignore
elif var_type.mro()[0] is tuple:
# Arg is a union of any possible values in the tuple.
return Union[get_args(iterable._var_type)] # type: ignore
return Union[get_args(var_type)] # type: ignore
else:
return get_args(iterable._var_type)[0]
return get_args(var_type)[0]
except Exception:
return Any

Expand Down
Loading