-
Notifications
You must be signed in to change notification settings - Fork 259
impl Convert to explicit imports #1589 #2192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this 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.
This comment has been minimized.
This comment has been minimized.
|
I'll give this one to @kinto0 since he's been thinking about star imports for the last couple weeks ;-) |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.