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

This commit is contained in:
Alek Petuskey 2023-02-12 21:47:21 -08:00 committed by GitHub
parent 6553be524f
commit adf5b7fb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -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,

View File

@ -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."
)