Skip to content

Commit 51a2d41

Browse files
mbeschastn0vclaude
andcommitted
test: cover scope walks for lambdas inside unpack-target sub-expressions
Two tests added to `unpack__ops.py` that drive the cell-var and referenced-name walks introduced for `Node::For` / `Node::With` targets: - `_for_target_lambda` puts a closure-capturing lambda inside the index of a for-loop subscript target. Without the walker added in `collect_cell_vars_from_unpack_target` / `collect_referenced_names_from_unpack_target`, the captured outer local would be invisible to scope analysis when nothing else in the enclosing function references it. - `_for_target_tuple_lambda` wraps the same pattern in a nested tuple target, exercising the `Tuple` recursion arm in both walkers. Together these close the codecov gap on the defense-in-depth scope walks that fire only when a subscript/attribute target's sub-expression contains a closure. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 87ce0eb commit 51a2d41

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

crates/monty/test_cases/unpack__ops.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,31 @@ def idx():
205205
(a[idx()],) = (7,)
206206
assert a == [7], 'side-effecting index produces correct store'
207207
assert calls == ['idx'], 'index expression evaluated exactly once at store time'
208+
209+
210+
# Lambda inside a `for`-loop subscript-target index — exercises the scope
211+
# walker that must promote captured outer locals to cell-vars when the only
212+
# reference to them lives inside the loop target's sub-expressions.
213+
def _for_target_lambda():
214+
x = 1
215+
y = [0, 0]
216+
for y[(lambda: x)()] in (10, 20):
217+
pass
218+
return y
219+
220+
221+
assert _for_target_lambda() == [0, 20], 'lambda capturing local from inside for-target subscript index'
222+
223+
224+
# Same pattern wrapped in a nested tuple target — exercises the Tuple
225+
# recursion arm in the cell-var / referenced-name unpack-target walkers.
226+
def _for_target_tuple_lambda():
227+
x = 1
228+
y = [0, 0]
229+
z = ''
230+
for y[(lambda: x)()], z in [(10, 'a'), (20, 'b')]:
231+
pass
232+
return y, z
233+
234+
235+
assert _for_target_tuple_lambda() == ([0, 20], 'b'), 'tuple target with lambda-in-subscript-index'

0 commit comments

Comments
 (0)