Skip to content

Commit fe0a28d

Browse files
authored
gh-122560: add test that comprehension loop var appears only in one scope of the symtable (#122582)
1 parent 4b63cd1 commit fe0a28d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Lib/test/test_symtable.py

+21
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,27 @@ def test_symtable_entry_repr(self):
528528
self.assertEqual(repr(self.top._table), expected)
529529

530530

531+
class ComprehensionTests(unittest.TestCase):
532+
def get_identifiers_recursive(self, st, res):
533+
res.extend(st.get_identifiers())
534+
for ch in st.get_children():
535+
self.get_identifiers_recursive(ch, res)
536+
537+
def test_loopvar_in_only_one_scope(self):
538+
# ensure that the loop variable appears only once in the symtable
539+
comps = [
540+
"[x for x in [1]]",
541+
"{x for x in [1]}",
542+
"{x:x*x for x in [1]}",
543+
]
544+
for comp in comps:
545+
with self.subTest(comp=comp):
546+
st = symtable.symtable(comp, "?", "exec")
547+
ids = []
548+
self.get_identifiers_recursive(st, ids)
549+
self.assertEqual(len([x for x in ids if x == 'x']), 1)
550+
551+
531552
class CommandLineTest(unittest.TestCase):
532553
maxDiff = None
533554

0 commit comments

Comments
 (0)