Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix comprehension scope not inheriting enclosing function's symtable
When a comprehension with a lambda creates a synthetic scope via
enter_scope(), the new scope got an empty symtable. This meant
variables from the enclosing function (e.g. parameters used as the
comprehension's iterable) were not accessible, causing
UnboundLocalError at runtime.

Fix by copying the parent symtable into the comprehension scope.
Since the comprehension is inlined (same basic blocks and registers),
the parent's register references remain valid.
  • Loading branch information
VaggelisD committed Mar 12, 2026
commit dedeba66bb666dd1e21709627e2df389cd6f4f79
6 changes: 5 additions & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,11 @@ def enter_scope(self, fn_info: FuncInfo) -> Iterator[None]:
so that the closure machinery sees a scope boundary.
"""
self.builders.append(self.builder)
self.symtables.append({})
# Copy the parent symtable so variables from the enclosing scope
# (e.g. function parameters used as the comprehension iterable)
# remain accessible. The comprehension is inlined (same basic blocks
# and registers), so the parent's register references are still valid.
self.symtables.append(dict(self.symtables[-1]))
self.runtime_args.append([])
self.fn_info = fn_info
self.fn_infos.append(self.fn_info)
Expand Down
20 changes: 20 additions & 0 deletions mypyc/test-data/run-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1472,3 +1472,23 @@ assert Foo.A["b"]() == "b"
def test_generator_with_lambda_default() -> None:
result = list((lambda i=i: i * 2) for i in range(4))
assert [f() for f in result] == [0, 2, 4, 6]

[case testLambdaInComprehensionWithParamIterable]
# Lambda inside comprehension where the iterable is a function parameter.
# The comprehension scope must be able to read variables from the enclosing
# function scope (not just the comprehension's own env class).
from typing import List, Callable

def transform(items: List[int]) -> List[Callable[[], int]]:
return [(lambda i=i: i * 2) for i in items] # type: ignore[misc]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add test where the name is different, e.g. lambda n=i: n * 2 (here and below)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


def uses_multiple_params(items: List[str], sep: str) -> List[Callable[[], str]]:
return [(lambda s=s: s + sep) for s in items] # type: ignore[misc]

def test_param_iterable() -> None:
funcs = transform([1, 2, 3])
assert [f() for f in funcs] == [2, 4, 6]

def test_multiple_params() -> None:
funcs = uses_multiple_params(["a", "b"], "!")
assert [f() for f in funcs] == ["a!", "b!"]
Loading