Skip to content

Commit 0b771db

Browse files
fixed bug in var type for iterable types (#2617)
* fixed bug in var type for iterable types * added test cases * formatting issue * fixed black formatting issues
1 parent 37f6620 commit 0b771db

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

reflex/vars.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,9 @@ def __getitem__(self, i: Any) -> Var:
623623

624624
# Get the type of the indexed var.
625625
if types.is_generic_alias(self._var_type):
626-
type_ = types.get_args(self._var_type)[0]
626+
index = i if not isinstance(i, Var) else 0
627+
type_ = types.get_args(self._var_type)
628+
type_ = type_[index % len(type_)]
627629
elif types._issubclass(self._var_type, str):
628630
type_ = str
629631

tests/test_var.py

+19
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,25 @@ def test_var_indexing_lists(var):
459459
assert str(var[-1]) == f"{{{var._var_name}.at(-1)}}"
460460

461461

462+
@pytest.mark.parametrize(
463+
"var, type_",
464+
[
465+
(BaseVar(_var_name="list", _var_type=List[int]), [int, int]),
466+
(BaseVar(_var_name="tuple", _var_type=Tuple[int, str]), [int, str]),
467+
],
468+
)
469+
def test_var_indexing_types(var, type_):
470+
"""Test that indexing returns valid types.
471+
472+
Args:
473+
var : The list, typle base var.
474+
type_ : The type on indexed object.
475+
476+
"""
477+
assert var[2]._var_type == type_[0]
478+
assert var[3]._var_type == type_[1]
479+
480+
462481
def test_var_indexing_str():
463482
"""Test that we can index into str vars."""
464483
str_var = BaseVar(_var_name="str", _var_type=str)

0 commit comments

Comments
 (0)