reflex
reflex copied to clipboard
Issue with `rx.foreach` for a computed var which is a `Union` of `str` and a `List[rx.Model]` type
Describe the bug
Error message: TypeError: You must provide an annotation for the state var _. Annotation cannot be typing.Any``
When a computed var is of type List[List[Union[str, List[Topics]]]] (where Topics is a rx.Model) rx.foreach fails to process the data.
To Reproduce Steps to reproduce the behavior:
- Code/Link to Repo:
# MyTopicsState(rx.State)
@rx.var
def get_topics(self) -> List[List[Union[str, List[Topics]]]]:
# <code>
# [["Parent Topic Name", [Topics(), Topics(), ...]], ["Parent Topic Name 2", [Topics(), Topics(), ...]], ...]
return topics
# frontend
rx.foreach(
MyTopicsState.get_topics,
render_topics
),
def render_topics(topics: List[Union[str, List[Topics]]]):
parent_topic_name: str = topics[0]
sub_topics: List[Topics] = topics[1]
return rx.box(
rx.vstack(
rx.text(parent_topic_name),
rx.foreach(
sub_topics,
to_ui_topic
),
),
width="100%",
)
def to_ui_topic(topic: Topics):
topic_name: str = topic.topic_name
return rx.box(
rx.text(topic_name),
width="100%",
)
Expected behavior A clear and concise description of what you expected to happen.
Screenshots If applicable, add screenshots to help explain your problem.
Specifics (please complete the following information):
- Python Version: 3.9
- Reflex Version: 0.2.0
- OS:
- Browser (Optional):
Additional context Add any other context about the problem here.
If I create 2 different computed vars rx.var, one for each type in the Union it works as expected. So I believe the issue must be with the complex type.
BaseVar should support Union type now, please try again with 0.3.3 or newer and reopen if the issue persists.