summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_listcomps.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_listcomps.py')
-rw-r--r--Lib/test/test_listcomps.py109
1 files changed, 107 insertions, 2 deletions
diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py
index 23e1b8c..985274d 100644
--- a/Lib/test/test_listcomps.py
+++ b/Lib/test/test_listcomps.py
@@ -200,7 +200,8 @@ class ListComprehensionTest(unittest.TestCase):
y = [g for x in [1]]
"""
outputs = {"y": [2]}
- self._check_in_scopes(code, outputs)
+ self._check_in_scopes(code, outputs, scopes=["module", "function"])
+ self._check_in_scopes(code, scopes=["class"], raises=NameError)
def test_inner_cell_shadows_outer_redefined(self):
code = """
@@ -328,7 +329,8 @@ class ListComprehensionTest(unittest.TestCase):
y = [x for [x ** x for x in range(x)][x - 1] in l]
"""
outputs = {"y": [3, 3, 3]}
- self._check_in_scopes(code, outputs)
+ self._check_in_scopes(code, outputs, scopes=["module", "function"])
+ self._check_in_scopes(code, scopes=["class"], raises=NameError)
def test_nested_3(self):
code = """
@@ -379,6 +381,109 @@ class ListComprehensionTest(unittest.TestCase):
with self.assertRaises(UnboundLocalError):
f()
+ def test_name_error_in_class_scope(self):
+ code = """
+ y = 1
+ [x + y for x in range(2)]
+ """
+ self._check_in_scopes(code, raises=NameError, scopes=["class"])
+
+ def test_global_in_class_scope(self):
+ code = """
+ y = 2
+ vals = [(x, y) for x in range(2)]
+ """
+ outputs = {"vals": [(0, 1), (1, 1)]}
+ self._check_in_scopes(code, outputs, ns={"y": 1}, scopes=["class"])
+
+ def test_in_class_scope_inside_function_1(self):
+ code = """
+ class C:
+ y = 2
+ vals = [(x, y) for x in range(2)]
+ vals = C.vals
+ """
+ outputs = {"vals": [(0, 1), (1, 1)]}
+ self._check_in_scopes(code, outputs, ns={"y": 1}, scopes=["function"])
+
+ def test_in_class_scope_inside_function_2(self):
+ code = """
+ y = 1
+ class C:
+ y = 2
+ vals = [(x, y) for x in range(2)]
+ vals = C.vals
+ """
+ outputs = {"vals": [(0, 1), (1, 1)]}
+ self._check_in_scopes(code, outputs, scopes=["function"])
+
+ def test_in_class_scope_with_global(self):
+ code = """
+ y = 1
+ class C:
+ global y
+ y = 2
+ # Ensure the listcomp uses the global, not the value in the
+ # class namespace
+ locals()['y'] = 3
+ vals = [(x, y) for x in range(2)]
+ vals = C.vals
+ """
+ outputs = {"vals": [(0, 2), (1, 2)]}
+ self._check_in_scopes(code, outputs, scopes=["module", "class"])
+ outputs = {"vals": [(0, 1), (1, 1)]}
+ self._check_in_scopes(code, outputs, scopes=["function"])
+
+ def test_in_class_scope_with_nonlocal(self):
+ code = """
+ y = 1
+ class C:
+ nonlocal y
+ y = 2
+ # Ensure the listcomp uses the global, not the value in the
+ # class namespace
+ locals()['y'] = 3
+ vals = [(x, y) for x in range(2)]
+ vals = C.vals
+ """
+ outputs = {"vals": [(0, 2), (1, 2)]}
+ self._check_in_scopes(code, outputs, scopes=["function"])
+
+ def test_nested_has_free_var(self):
+ code = """
+ items = [a for a in [1] if [a for _ in [0]]]
+ """
+ outputs = {"items": [1]}
+ self._check_in_scopes(code, outputs, scopes=["class"])
+
+ def test_nested_free_var_not_bound_in_outer_comp(self):
+ code = """
+ z = 1
+ items = [a for a in [1] if [x for x in [1] if z]]
+ """
+ self._check_in_scopes(code, {"items": [1]}, scopes=["module", "function"])
+ self._check_in_scopes(code, {"items": []}, ns={"z": 0}, scopes=["class"])
+
+ def test_nested_free_var_in_iter(self):
+ code = """
+ items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]]
+ """
+ self._check_in_scopes(code, {"items": [1]})
+
+ def test_nested_free_var_in_expr(self):
+ code = """
+ items = [(_C, [x for x in [1] if _C]) for _C in [0, 1]]
+ """
+ self._check_in_scopes(code, {"items": [(0, []), (1, [1])]})
+
+ def test_nested_listcomp_in_lambda(self):
+ code = """
+ f = [(z, lambda y: [(x, y, z) for x in [3]]) for z in [1]]
+ (z, func), = f
+ out = func(2)
+ """
+ self._check_in_scopes(code, {"z": 1, "out": [(3, 2, 1)]})
+
__test__ = {'doctests' : doctests}