]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122560: add test that comprehension loop var appears only in one scope of the...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Fri, 2 Aug 2024 22:56:51 +0000 (23:56 +0100)
committerGitHub <noreply@github.com>
Fri, 2 Aug 2024 22:56:51 +0000 (23:56 +0100)
Lib/test/test_symtable.py

index bd367c1591c7441498c355258f77532913c89eb5..24d89b09d946ad3b5a85de33f583b66bd8cdc42a 100644 (file)
@@ -528,6 +528,27 @@ class SymtableTest(unittest.TestCase):
         self.assertEqual(repr(self.top._table), expected)
 
 
+class ComprehensionTests(unittest.TestCase):
+    def get_identifiers_recursive(self, st, res):
+        res.extend(st.get_identifiers())
+        for ch in st.get_children():
+            self.get_identifiers_recursive(ch, res)
+
+    def test_loopvar_in_only_one_scope(self):
+        # ensure that the loop variable appears only once in the symtable
+        comps = [
+            "[x for x in [1]]",
+            "{x for x in [1]}",
+            "{x:x*x for x in [1]}",
+        ]
+        for comp in comps:
+            with self.subTest(comp=comp):
+                st = symtable.symtable(comp, "?", "exec")
+                ids = []
+                self.get_identifiers_recursive(st, ids)
+                self.assertEqual(len([x for x in ids if x == 'x']), 1)
+
+
 class CommandLineTest(unittest.TestCase):
     maxDiff = None