Skip to content

fix: give module-level walrus targets in comprehensions a full symbol - #251

Open
AbuYahya94 wants to merge 1 commit into
sourcegraph:scipfrom
AbuYahya94:fix-walrus-module-comprehension
Open

AbuYahya94 wants to merge 1 commit into
sourcegraph:scipfrom
AbuYahya94:fix-walrus-module-comprehension

Conversation

@AbuYahya94

Copy link
Copy Markdown

Fixes #248.

What. When an assignment expression (:=) sits inside a list, set or dict comprehension or a generator expression at module scope, its target was emitted with a bare descriptor (y., z., k., w.) and no scheme, manager, package or version. scip lint fails to parse those symbols ("reached end of symbol while parsing ").

Why. PEP 572 binds such a target in the scope that contains the comprehension. makeScipSymbol resolved the name's scope by walking up the parent chain. Inside a function that path stops early and makes a local, and at module level inside a suite (e.g. an if body) it reaches the module. In a top-level statement it reached the ListComprehension node, which returns ScipSymbol.empty(), so the term was built on an empty parent.

How. For a name that is the target of an AssignmentExpression, ask pyright for the binding scope with ParseTreeUtils.getEvaluationNodeForAssignmentExpression. When that is the module, build the term on the module symbol (the same symbol a plain module-level walrus such as if (n := len(xs)) already gets). Function and lambda targets fall through to the existing local handling, so they don't change.

Tests.

  • New snapshot snapshots/input/walrus_comprehension/walrus.py: list, set, dict and generator comprehensions plus a walrus in the element expression, a later read of each target, and a function-scoped case that stays local.
  • The existing snapshots/output/unique/walrus.py had the bug recorded (definition any_n.). It now reads snapshot-util 0.1 walrus/any_n.. That is the only change to an existing snapshot.
  • npm run check-snapshots: 23/23 pass. npm test: 7/7 pass. npm run check:prettier: clean. Run locally on macOS arm64, Node 24.5.0, Python 3.10.22 (matches .tool-versions).
  • The repro from the issue, run through scip lint (scip CLI built from scip-code/scip main): before, every comprehension target fails to parse; after, all symbols parse, e.g. scip-python python walrus-repro 0.1.0 toplevel/y..

Not in this PR. scip lint still reports "no matching SymbolInformation" for module-level walrus targets, including the plain if (n := ...) case that already had a full symbol. That gap predates this change, so I left it for a separate PR.

Note on the local test run: pythonCLITests fails on a fresh machine because pip install -e now produces an __editable___*_finder.py instead of copying __init__.py (the subject of #214). I swapped in a non-editable install locally to run the suite and didn't commit it.

AI disclosure (recommended; the repo has no rule requiring it): I wrote this fix with help from Claude Code and reviewed and tested it myself.

This fix was written with Claude Code; the tests above were run locally.

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 sourcegraph#248

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Walrus target inside a module-level comprehension gets a malformed symbol (e.g. y.)

1 participant