diff options
author | Carl Meyer <carl@oddbird.net> | 2023-05-19 00:07:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-19 00:07:35 (GMT) |
commit | 86e6f16ccb97f66f2b9a31191ce347dca499d48c (patch) | |
tree | 32a0860742245c2d9a5cc509716d747ad5563594 /Lib/test/test_listcomps.py | |
parent | 3fadd7d5857842fc5cddd4c496b73161b0bcb421 (diff) | |
download | cpython-86e6f16ccb97f66f2b9a31191ce347dca499d48c.zip cpython-86e6f16ccb97f66f2b9a31191ce347dca499d48c.tar.gz cpython-86e6f16ccb97f66f2b9a31191ce347dca499d48c.tar.bz2 |
gh-104602: ensure all cellvars are known up front (#104603)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_listcomps.py')
-rw-r--r-- | Lib/test/test_listcomps.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py index ae63f4a..fdd2d66 100644 --- a/Lib/test/test_listcomps.py +++ b/Lib/test/test_listcomps.py @@ -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 |