Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type checking in for loops #2096

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions vyper/context/validation/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ def visit_For(self, node):
call_node,
)

for_loop_exceptions = []
for type_ in type_list:
# type check the for loop body using each possible type for iterator value
type_ = copy.deepcopy(type_)
type_.is_immutable = True

with self.namespace.enter_scope():
try:
self.namespace[node.target.id] = type_
Expand All @@ -365,10 +368,13 @@ def visit_For(self, node):
try:
for n in node.body:
self.visit(n)
return
return
except TypeMismatch as exc:
if len(type_list) == 1:
raise exc
for_loop_exceptions.append(exc)

if len(set(str(i) for i in for_loop_exceptions)) == 1:
# if every attempt at type checking raised the same exception,
raise for_loop_exceptions[0]

raise TypeMismatch("Could not determine type for iterator values", node)

Expand Down