reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Cannot render states with typing.ForwardRef

Open guidocalvano opened this issue 8 months ago β€’ 5 comments

Describe the bug I tried to render a tree, but kept getting missing attribute errors whenever I had a ForwardRef in my state.

To Reproduce Steps to reproduce the behavior:

  • Code/Link to Repo:

class ASTNodeState(rx.Base):
    key: str = "unknown"
    type: str = "unknown"
    text: str = "loading"
    children: List["ASTNodeState"] = []


class ASTNodeView(rx.ComponentState):

    @classmethod
    def get_component(cls, children: List[ASTNodeState], key: str, type: str, text: str) -> rx.Component:

        return rx.chakra.span(rx.text(key), rx.chakra.text(type), rx.chakra.text(text),
                              rx.foreach(children, render_child))

ast_node_view = ASTNodeView.create

def render_child(item: ASTNodeState):
    # hack to make recursion possible, this leads to an infinite loop in some deep copy function
    # item.__var_type = ASTNodeState
    # hack that I tried to use to derive type of the member (by also hacking in the Var.__getattr__ function)
    # item.___actual_class = ASTNodeState
    # final hack I tried, that also didn't work
    # other_item: ASTNodeState = item
    return ast_node_view(key=item.key, type=item.type, text=item.text, children=item.children)


Expected behavior I expected a beautiful tree structure to render...

Specifics (please complete the following information):

  • Python Version: 3.11 and 3.9 I tried
  • Reflex Version: reflex==0.5.2
  • OS: Ubuntu
  • Browser (Optional): Chrome, but the problem seems to occur in the python code.

Additional context When I hack the __var_type to the correct class I get infinite loops. I tried to create a member with the actual class and use that to detect type, but that for some weird reason raised an exception when run (but not when evaluated in my debugger).

The end of a frustrating day...

Weird stuff...

guidocalvano avatar Jun 04 '24 18:06 guidocalvano