Import refactoring package and pylsp plugin
Project description
Starkiller
Work in progress
An import refactoring package and python-lsp-server plugin.
Starkiller aims to be static, i.e. to analyse source code without actually executing it, and fast, thanks to built-in
ast module.
The initial goal was to create a simple linter to get rid of star imports, hence the choice of the package name.
Using as a package
Starkiller can be used as a package for import refactoring. Each public method and class has a docstring explaining what it does and how to use it.
Python LSP Server plugin
The pylsp plugin provides the following code actions to refactor import statements:
Replace * with explicit names- suggested forfrom ... import *statements.Replace * import with module import- suggested forfrom ... import *statements.Replace from import with module import- suggested forfrom ... import ...statements.Replace module import with from import- suggested forimport ...statements.Remove unnecessary import- suggested forimportstatements with unused names.
To enable the plugin install Starkiller in the same virtual environment as python-lsp-server with [pylsp] optional
dependency. E.g., with pipx:
pipx inject python-lsp-server starkiller[pylsp]
The plugin is enabled just the same way as any other pylsp plugin. E.g., in Neovim via
lspconfig:
require("lspconfig").pylsp.setup {
settings = {
pylsp = {
plugins = {
starkiller = { enabled = true },
aliases = {
numpy = "np",
[ "matplotlib.pyplot" ] = "plt",
}
}
}
}
}
Comma separated package imports
Multiple package imports like in the following example do not trigger any Code Actions right now:
import os, sys
This is hard to understand which import the user wants to fix here: os, sys or both. Splitting imports to different
lines would help, but the user has to do it manually or with some other tool like Ruff.
Starkiller is not a code formatter and should not handle import splitting.
At some point this might change. For example, a separate Code Action for each package could be suggested.
Alternatives and inspiration
- removestar is a Pyflakes based tool with similar objectives.
- SurpriseDog's scripts are a great source of inspiration.
pylspitself has a built-inrope_autoimportplugin utilizing Rope'sautoimportmodule.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file starkiller-0.1.3.tar.gz.
File metadata
- Download URL: starkiller-0.1.3.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
780f6c7bdd6f021abccb0e0dc8ee87c3e0ff7da14d4e73582924ac92042824cc
|
|
| MD5 |
2acbc8862482fd5962c33d3660229e27
|
|
| BLAKE2b-256 |
503ad88eeda83f56e2c417c5f309933b29ef4977d505054575d4d84a6c2e9541
|
File details
Details for the file starkiller-0.1.3-py3-none-any.whl.
File metadata
- Download URL: starkiller-0.1.3-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a9f9192661988336ffc27a1b9c93fef7e57396d0aef8463bc15488756d96cb0
|
|
| MD5 |
21fc52794fab934092e2a520d5bb4863
|
|
| BLAKE2b-256 |
74d0e70991f186840ad2aca89a6f7d67b2cbbddcf640911370da6c6d0d8133c0
|