0
0
mirror of https://github.com/python/cpython.git synced 2024-11-21 12:59:38 +01:00

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

This commit is contained in:
Irit Katriel 2024-08-02 23:56:51 +01:00 committed by GitHub
parent 4b63cd170e
commit fe0a28d850
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View 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