Skip to content

Commit

Permalink
pythongh-122560: add test that comprehension loop var appears only in…
Browse files Browse the repository at this point in the history
… one scope of the symtable
  • Loading branch information
iritkatriel committed Aug 1, 2024
1 parent fda6bd8 commit b58bc21
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b58bc21

Please sign in to comment.