Skip to content

Commit c1e725f

Browse files
committed
fixed local name detection for comprehensions
1 parent 278204a commit c1e725f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

custom_components/pyscript/eval.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,10 @@ async def get_names_set(self, arg, names, nonlocal_names=None, global_names=None
17081708
for name in await self.get_target_names(item.optional_vars):
17091709
local_names.add(name)
17101710
names.add(name)
1711+
elif cls_name in {"ListComp", "DictComp", "SetComp"}:
1712+
target_vars, _ = await self.loopvar_scope_save(arg.generators)
1713+
for name in target_vars:
1714+
local_names.add(name)
17111715
elif cls_name == "Try":
17121716
for handler in arg.handlers:
17131717
if handler.name is not None:

tests/test_unit_eval.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ def no_op(i):
254254
"{f'{i}+{k}':i+k for i in range(3) for k in range(3) if k <= i}",
255255
{"0+0": 0, "1+0": 1, "1+1": 2, "2+0": 2, "2+1": 3, "2+2": 4},
256256
],
257+
["def f(l=5): return [i for i in range(l)];\nf(6)", [0, 1, 2, 3, 4, 5]],
258+
["def f(l=5): return {i+j for i,j in zip(range(l), range(l))};\nf()", {0, 2, 4, 6, 8}],
259+
[
260+
"def f(l=5): return {i:j for i,j in zip(range(l), range(1,l+1))};\nf()",
261+
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5},
262+
],
257263
[
258264
"""
259265
d = {"x": 1, "y": 2, "z": 3}

0 commit comments

Comments
 (0)