Repository navigation
fix: give module-level walrus targets in comprehensions a full symbol - #251
Open
AbuYahya94 wants to merge 1 commit into
Open
AbuYahya94 wants to merge 1 commit into
AbuYahya94 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 lintfails to parse those symbols ("reached end of symbol while parsing ").Why. PEP 572 binds such a target in the scope that contains the comprehension.
makeScipSymbolresolved 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. anifbody) it reaches the module. In a top-level statement it reached theListComprehensionnode, which returnsScipSymbol.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 withParseTreeUtils.getEvaluationNodeForAssignmentExpression. When that is the module, build the term on the module symbol (the same symbol a plain module-level walrus such asif (n := len(xs))already gets). Function and lambda targets fall through to the existing local handling, so they don't change.Tests.
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.snapshots/output/unique/walrus.pyhad the bug recorded (definition any_n.). It now readssnapshot-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).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 lintstill reports "no matching SymbolInformation" for module-level walrus targets, including the plainif (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:
pythonCLITestsfails on a fresh machine becausepip install -enow produces an__editable___*_finder.pyinstead 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.