gh-104602: ensure all cellvars are known up front (#104603)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Carl Meyer 2023-05-18 18:07:35 -06:00 committed by GitHub
parent 3fadd7d585
commit 86e6f16ccb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 22 deletions

View file

@ -381,6 +381,32 @@ class ListComprehensionTest(unittest.TestCase):
with self.assertRaises(UnboundLocalError):
f()
def test_global_outside_cellvar_inside_plus_freevar(self):
code = """
a = 1
def f():
func, = [(lambda: b) for b in [a]]
return b, func()
x = f()
"""
self._check_in_scopes(
code, {"x": (2, 1)}, ns={"b": 2}, scopes=["function", "module"])
# inside a class, the `a = 1` assignment is not visible
self._check_in_scopes(code, raises=NameError, scopes=["class"])
def test_cell_in_nested_comprehension(self):
code = """
a = 1
def f():
(func, inner_b), = [[lambda: b for b in c] + [b] for c in [[a]]]
return b, inner_b, func()
x = f()
"""
self._check_in_scopes(
code, {"x": (2, 2, 1)}, ns={"b": 2}, scopes=["function", "module"])
# inside a class, the `a = 1` assignment is not visible
self._check_in_scopes(code, raises=NameError, scopes=["class"])
def test_name_error_in_class_scope(self):
code = """
y = 1