Compare commits

...

2 Commits

View File

@@ -439,8 +439,10 @@ class PythonTyper(
# print(m) # <- m is still defined # print(m) # <- m is still defined
test_type: Type = self.type_of(stmt.test) test_type: Type = self.type_of(stmt.test)
# TODO Allow subtypes or any type if (
if test_type != self.types.get_type("bool"): not self.types.is_subtype(test_type, self.types.get_type("bool"))
and test_type != UnknownType()
):
self.reporter.error( self.reporter.error(
stmt.test.location, f"If test must be a boolean, got {test_type}" stmt.test.location, f"If test must be a boolean, got {test_type}"
) )
@@ -456,13 +458,16 @@ class PythonTyper(
pass pass
def visit_for_stmt(self, stmt: p.ForStmt) -> None: def visit_for_stmt(self, stmt: p.ForStmt) -> None:
item_type: Optional[Type] = self._get_iterator_type(stmt.iterator) item_type: Type = UnknownType()
if item_type is None: iterator_type: Type = self.type_of(stmt.iterator)
iterator_type: Type = self.compute_type(stmt.iterator) if iterator_type != UnknownType():
maybe_item_type = self._get_iterator_type(stmt.iterator, iterator_type)
if maybe_item_type is None:
self.reporter.error( self.reporter.error(
stmt.iterator.location, f"{iterator_type} is not iterable" stmt.iterator.location, f"{iterator_type} is not iterable"
) )
item_type = UnknownType() else:
item_type = maybe_item_type
self._assign(stmt.location, stmt.target, item_type) self._assign(stmt.location, stmt.target, item_type)
self.judge(stmt.target, item_type) self.judge(stmt.target, item_type)
@@ -638,7 +643,10 @@ class PythonTyper(
test_type: Type = self.type_of(expr.test) test_type: Type = self.type_of(expr.test)
# TODO Allow subtypes or any type # TODO Allow subtypes or any type
if test_type != self.types.get_type("bool"): if (
not self.is_subtype(test_type, self.types.get_type("bool"))
and test_type != UnknownType()
):
self.reporter.error( self.reporter.error(
expr.test.location, f"If test must be a boolean, got {test_type}" expr.test.location, f"If test must be a boolean, got {test_type}"
) )
@@ -1150,9 +1158,8 @@ class PythonTyper(
return False return False
return True return True
def _get_iterator_type(self, expr: p.Expr) -> Optional[Type]: def _get_iterator_type(self, expr: p.Expr, type: Type) -> Optional[Type]:
# TODO: lookup __iter__ # TODO: lookup __iter__
type: Type = self.type_of(expr)
getitem: Optional[Type] = self.types.lookup_member(type, "__getitem__") getitem: Optional[Type] = self.types.lookup_member(type, "__getitem__")
if getitem is None: if getitem is None:
return None return None