Skip to content

Tighten import handling and fix SetComprehension typo#9607

Open
manzt wants to merge 2 commits into
mainfrom
push-xkrmzlkmskmo
Open

Tighten import handling and fix SetComprehension typo#9607
manzt wants to merge 2 commits into
mainfrom
push-xkrmzlkmskmo

Conversation

@manzt
Copy link
Copy Markdown
Collaborator

@manzt manzt commented May 19, 2026

Follow up to #9379

ImportStatement was collecting every VariableName direct child as a binding, including the module path in from m import ... and the imported name when shadowed by as.

Also:

  • Removed the dead ImportFromStatement branch — the Lezer Python grammar only emits ImportStatement, so it never matched.
  • Fixed pre-existing SetComprehension typo in reactive-references/analyzer.ts (grammar node is SetComprehensionExpression); set comprehensions now correctly create a scope.

Follow up to #9379

`ImportStatement` was collecting every `VariableName` direct child as a
binding, including the module path in `from m import ...` and the
imported name when shadowed by `as`.

Also:

- Removed the dead `ImportFromStatement` branch — the Lezer Python grammar
  only emits `ImportStatement`, so it never matched.
- Fixed pre-existing `SetComprehension` typo in reactive-references/analyzer.ts
  (grammar node is `SetComprehensionExpression`); set comprehensions now
  correctly create a scope.
Copilot AI review requested due to automatic review settings May 19, 2026 15:30
@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 19, 2026 3:32pm

Request Review

@manzt manzt added the enhancement New feature or request label May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens Python import handling in the CodeMirror AST analyzers used for reactive-variable highlighting and go-to-definition, and fixes a grammar-node typo so set comprehensions correctly create a scope.

Changes:

  • Fix set comprehension scoping by using the correct Lezer node name (SetComprehensionExpression).
  • Refine ImportStatement binding collection so from m import x as y doesn’t treat m or x as local bindings (only y binds).
  • Prevent import-statement names from being treated as reactive uses.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
frontend/src/core/codemirror/reactive-references/analyzer.ts Fixes set-comprehension scope creation; improves import binding collection; skips import nodes during reactive-use scan.
frontend/src/core/codemirror/reactive-references/tests/analyzer.test.ts Adds regression tests for set-comprehension scoping and from-import binding/reactive behavior.
frontend/src/core/codemirror/go-to-definition/commands.ts Improves import binding parsing for scoped declaration collection; adjusts fallback behavior when usagePosition is provided.
frontend/src/core/codemirror/go-to-definition/tests/utils.test.ts Adds regression test ensuring cross-cell resolution isn’t blocked by module-path “declarations” in from-imports.
frontend/src/core/codemirror/go-to-definition/tests/commands.test.ts Adds tests covering from-import alias binding semantics and module-path non-binding behavior.

Comment on lines +314 to +321
if (subCursor.name === "VariableName") {
commit();
pending = options.state.doc.sliceString(
subCursor.from,
subCursor.to,
);
// Add to the current innermost scope
const currentScope =
currentScopeStack[currentScopeStack.length - 1] ?? -1;
if (!allDeclarations.has(currentScope)) {
allDeclarations.set(currentScope, new Set());
}
allDeclarations.get(currentScope)?.add(varName);
} else if (subCursor.name === ",") {
commit();
Comment on lines +341 to 352
if (subCursor.name === "VariableName") {
// Flush any previous pending name (no `as` followed it).
commit();
pending = {
from: subCursor.from,
matches:
state.doc.sliceString(subCursor.from, subCursor.to) ===
variableName,
};
} else if (subCursor.name === ",") {
commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants