Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
xs = [1, 2, 3]

if (n := len(xs)) > 0:
pass

# PEP 572: the target of an assignment expression inside a comprehension
# binds in the scope that contains the comprehension, here the module.
a = [y for x in xs if (y := x * 2) > 1]
b = {z for x in xs if (z := x + 1)}
c = {k: k for x in xs if (k := str(x))}
d = list(w for x in xs if (w := x - 1))
e = [(v := x) for x in xs]
print(y, z, k, w, v)


def f():
# Inside a function the target is a local of the function.
return [q for x in xs if (q := x)]
4 changes: 2 additions & 2 deletions packages/pyright-scip/snapshots/output/unique/walrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def show_some_comprehension():
# if + comprehension
if any((any_n := num) < 0 for num in some_list):
# ^^^ reference local 4
# ^^^^^ definition any_n.
# ^^^^^ definition snapshot-util 0.1 walrus/any_n.
# ^^^ reference local 5
# ^^^ definition local 5
# ^^^^^^^^^ reference snapshot-util 0.1 walrus/some_list.
print(f"Negative number found: {any_n}")
# ^^^^^ reference python-stdlib 3.11 builtins/print().
# ^^^^^ reference any_n.
# ^^^^^ reference snapshot-util 0.1 walrus/any_n.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# < definition scip-python python snapshot-util 0.1 walrus/__init__:

xs = [1, 2, 3]
#^ definition snapshot-util 0.1 walrus/xs.

if (n := len(xs)) > 0:
# ^ definition snapshot-util 0.1 walrus/n.
# ^^^ reference local 0
# ^^ reference snapshot-util 0.1 walrus/xs.
pass

# PEP 572: the target of an assignment expression inside a comprehension
# binds in the scope that contains the comprehension, here the module.
a = [y for x in xs if (y := x * 2) > 1]
# definition snapshot-util 0.1 walrus/a.
# ^ reference snapshot-util 0.1 walrus/y.
# ^ definition local 1
# ^^ reference snapshot-util 0.1 walrus/xs.
# ^ definition snapshot-util 0.1 walrus/y.
# ^ reference local 1
b = {z for x in xs if (z := x + 1)}
# definition snapshot-util 0.1 walrus/b.
# ^ reference snapshot-util 0.1 walrus/z.
# ^ definition local 2
# ^^ reference snapshot-util 0.1 walrus/xs.
# ^ definition snapshot-util 0.1 walrus/z.
# ^ reference local 2
c = {k: k for x in xs if (k := str(x))}
# definition snapshot-util 0.1 walrus/c.
# ^ reference snapshot-util 0.1 walrus/k.
# ^ reference snapshot-util 0.1 walrus/k.
# ^ definition local 3
# ^^ reference snapshot-util 0.1 walrus/xs.
# ^ definition snapshot-util 0.1 walrus/k.
# ^^^ reference python-stdlib 3.11 builtins/str#
# ^ reference local 3
d = list(w for x in xs if (w := x - 1))
# definition snapshot-util 0.1 walrus/d.
# ^^^^ reference python-stdlib 3.11 builtins/list#
# ^ reference snapshot-util 0.1 walrus/w.
# ^ definition local 4
# ^^ reference snapshot-util 0.1 walrus/xs.
# ^ definition snapshot-util 0.1 walrus/w.
# ^ reference local 4
e = [(v := x) for x in xs]
# definition snapshot-util 0.1 walrus/e.
# ^ definition snapshot-util 0.1 walrus/v.
# ^ reference local 5
# ^ definition local 5
# ^^ reference snapshot-util 0.1 walrus/xs.
print(y, z, k, w, v)
#^^^^ reference python-stdlib 3.11 builtins/print().
# ^ reference snapshot-util 0.1 walrus/y.
# ^ reference snapshot-util 0.1 walrus/z.
# ^ reference snapshot-util 0.1 walrus/k.
# ^ reference snapshot-util 0.1 walrus/w.
# ^ reference snapshot-util 0.1 walrus/v.


def f():
# ^ definition snapshot-util 0.1 walrus/f().
# Inside a function the target is a local of the function.
return [q for x in xs if (q := x)]
# ^ reference local 6
# ^ definition local 7
# ^^ reference snapshot-util 0.1 walrus/xs.
# ^ definition local 6
# ^ reference local 7

12 changes: 12 additions & 0 deletions packages/pyright-scip/src/treeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,18 @@ export class TreeVisitor extends ParseTreeWalker {
}
}

// PEP 572: the target of an assignment expression inside a
// comprehension binds in the scope containing the comprehension.
// Walking up through the parents would stop at the
// ListComprehension node, which has no symbol of its own, so
// resolve module-level targets against the module directly.
if (parent.nodeType === ParseNodeType.AssignmentExpression && parent.name === node) {
const evaluationNode = ParseTreeUtils.getEvaluationNodeForAssignmentExpression(parent);
if (evaluationNode?.nodeType === ParseNodeType.Module) {
return Symbols.makeTerm(this.getScipSymbol(evaluationNode), node.value);
}
}

const enclosingSuite = ParseTreeUtils.getEnclosingSuite(node);
if (enclosingSuite) {
// Certain enclosing suites contain their scope completely.
Expand Down