Przeglądaj źródła

Added Type Annotation Error Message Indexing/Foreach (#530)

Alek Petuskey 2 lat temu
rodzic
commit
adf5b7fb1b
2 zmienionych plików z 11 dodań i 0 usunięć
  1. 7 0
      pynecone/components/layout/foreach.py
  2. 4 0
      pynecone/var.py

+ 7 - 0
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,

+ 4 - 0
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."
             )