From adf5b7fb1ba0e7e0bc1df3431976f291ff8a9b00 Mon Sep 17 00:00:00 2001 From: Alek Petuskey Date: Sun, 12 Feb 2023 21:47:21 -0800 Subject: [PATCH] Added Type Annotation Error Message Indexing/Foreach (#530) --- pynecone/components/layout/foreach.py | 7 +++++++ pynecone/var.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/pynecone/components/layout/foreach.py b/pynecone/components/layout/foreach.py index 0920ed202..a06242136 100644 --- a/pynecone/components/layout/foreach.py +++ b/pynecone/components/layout/foreach.py @@ -28,11 +28,18 @@ class Foreach(Component): Returns: The foreach component. + + Raises: + TypeError: If the iterable is of type Any. """ try: type_ = iterable.type_.__args__[0] except Exception: type_ = Any + if iterable.type_ == Any: + raise TypeError( + f"Could not foreach over var of type Any. (If you are trying to foreach over a state var, add a type annotation to the var.)" + ) arg = BaseVar(name="_", type_=type_, is_local=True) return cls( iterable=iterable, diff --git a/pynecone/var.py b/pynecone/var.py index 6a0d31283..2272c0aca 100644 --- a/pynecone/var.py +++ b/pynecone/var.py @@ -149,6 +149,10 @@ class Var(ABC): utils._issubclass(self.type_, Union[List, Dict]) or utils.is_dataframe(self.type_) ): + if self.type_ == Any: + raise TypeError( + f"Could not index into var of type Any. (If you are trying to index into a state var, add a type annotation to the var.)" + ) raise TypeError( f"Var {self.name} of type {self.type_} does not support indexing." )