Skip to content

Commit ed9ba4f

Browse files
committed
Use global symbol table if 1st non-comprehension scope
1 parent 822813d commit ed9ba4f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mypy/semanal.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4542,9 +4542,15 @@ def current_symbol_table(self, escape_comprehensions: bool = False) -> SymbolTab
45424542
if self.is_func_scope():
45434543
assert self.locals[-1] is not None
45444544
if escape_comprehensions:
4545+
assert len(self.locals) == len(self.is_comprehension_stack)
4546+
# Retrieve the symbol table from the enclosing non-comprehension scope.
45454547
for i, is_comprehension in enumerate(reversed(self.is_comprehension_stack)):
45464548
if not is_comprehension:
4547-
names = self.locals[-1 - i]
4549+
if i == len(self.locals) - 1: # The last iteration.
4550+
# The caller of the comprehension is in the global space.
4551+
names = self.globals
4552+
else:
4553+
names = cast(SymbolTable, self.locals[-1 - i])
45484554
break
45494555
else:
45504556
assert False, "Should have at least one non-comprehension scope"

0 commit comments

Comments
 (0)