From ffc2fea95eede88dbb21a120c254bab3ad20664b Mon Sep 17 00:00:00 2001 From: Hamza Abdul-Tawwaab Date: Tue, 24 Nov 2026 23:54:08 +0000 Subject: [PATCH] fix: give module-level walrus targets in comprehensions a full symbol PEP 572 binds the target of an assignment expression inside a comprehension in the scope that contains the comprehension. At module scope, scip-python resolved the target's scope by walking up the parent chain, reached the ListComprehension node, which has no symbol, and emitted a bare descriptor such as `y.` with no scheme, package or version. `scip lint` rejects those symbols. Use pyright's getEvaluationNodeForAssignmentExpression to find the binding scope, and when it is the module, build the term from the module symbol. Targets inside functions and lambdas stay locals, as before. The existing `unique/walrus.py` snapshot had the malformed `any_n.` symbol recorded; it now reads `walrus/any_n.`. Fixes #248 Co-Authored-By: Claude Opus 5.5 --- .../input/walrus_comprehension/walrus.py | 18 +++++ .../snapshots/output/unique/walrus.py | 4 +- .../output/walrus_comprehension/walrus.py | 69 +++++++++++++++++++ packages/pyright-scip/src/treeVisitor.ts | 12 ++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 packages/pyright-scip/snapshots/input/walrus_comprehension/walrus.py create mode 100644 packages/pyright-scip/snapshots/output/walrus_comprehension/walrus.py diff --git a/packages/pyright-scip/snapshots/input/walrus_comprehension/walrus.py b/packages/pyright-scip/snapshots/input/walrus_comprehension/walrus.py new file mode 100644 index 000000000..58d8ccd2c --- /dev/null +++ b/packages/pyright-scip/snapshots/input/walrus_comprehension/walrus.py @@ -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)] diff --git a/packages/pyright-scip/snapshots/output/unique/walrus.py b/packages/pyright-scip/snapshots/output/unique/walrus.py index 535038ba4..6c0ecae51 100644 --- a/packages/pyright-scip/snapshots/output/unique/walrus.py +++ b/packages/pyright-scip/snapshots/output/unique/walrus.py @@ -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. diff --git a/packages/pyright-scip/snapshots/output/walrus_comprehension/walrus.py b/packages/pyright-scip/snapshots/output/walrus_comprehension/walrus.py new file mode 100644 index 000000000..9b50bb5de --- /dev/null +++ b/packages/pyright-scip/snapshots/output/walrus_comprehension/walrus.py @@ -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 + diff --git a/packages/pyright-scip/src/treeVisitor.ts b/packages/pyright-scip/src/treeVisitor.ts index a7f4d16ad..0c0c9ddf6 100644 --- a/packages/pyright-scip/src/treeVisitor.ts +++ b/packages/pyright-scip/src/treeVisitor.ts @@ -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.