Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes part of #1589

Implemented “Convert to explicit imports” as a refactor code action: it locates the selected from ... import *, resolves actual used names via bindings, rewrites the import line (preserving indentation and trailing comments), wires it into LSP capabilities.

Test Plan

Adds coverage tests.

@meta-cla meta-cla bot added the cla signed label Jan 22, 2026
@github-actions

This comment has been minimized.

@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 22, 2026 12:01
Copilot AI review requested due to automatic review settings January 22, 2026 12:01
Copy link

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 implements the "Convert to explicit imports" refactoring code action to help developers replace wildcard imports (from ... import *) with explicit imports listing only the names actually used in the code. This addresses part of issue #1589 for feature parity with Pylance/Basedpyright.

Changes:

  • Adds new refactor code action that converts star imports to explicit import lists by analyzing bindings
  • Registers new "refactor.rewrite" code action kind in LSP server capabilities
  • Preserves indentation, trailing comments (like # noqa), and handles both absolute and relative imports

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pyrefly/lib/state/lsp/quick_fixes/convert_star_import.rs Core implementation: finds star imports, collects used names via AST visitor and bindings, generates replacement text with proper formatting
pyrefly/lib/state/lsp.rs Adds transaction method wrapper for the new code action
pyrefly/lib/state/lsp/quick_fixes/mod.rs Declares the new module
pyrefly/lib/lsp/non_wasm/server.rs Registers "refactor.rewrite" capability and wires the action into the code action handler
pyrefly/lib/test/lsp/lsp_interaction/basic.rs Updates capability test expectations to include new code action kind
pyrefly/lib/test/lsp/code_actions.rs Adds comprehensive tests covering basic usage, relative imports, multiple star imports, and no-action cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

This comment has been minimized.

@yangdanny97
Copy link
Contributor

I'll give this one to @kinto0 since he's been thinking about star imports for the last couple weeks ;-)

@github-actions

This comment has been minimized.

@github-actions
Copy link

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants