https://github.com/python/cpython/commit/fe0a28d850943cf2ba132c9b0a933bb0c98ff0ae
commit: fe0a28d850943cf2ba132c9b0a933bb0c98ff0ae
branch: main
author: Irit Katriel <[email protected]>
committer: iritkatriel <[email protected]>
date: 2024-08-02T23:56:51+01:00
summary:

gh-122560: add test that comprehension loop var appears only in one scope of 
the symtable (#122582)

files:
M Lib/test/test_symtable.py

diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py
index bd367c1591c744..24d89b09d946ad 100644
--- a/Lib/test/test_symtable.py
+++ b/Lib/test/test_symtable.py
@@ -528,6 +528,27 @@ def test_symtable_entry_repr(self):
         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
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to