Notebook imports are incorrectly removes imports with "source.fixAll": "explicit"
· Issue #391 · astral-sh/ruff-vscode (original) (raw)
When using notebook.codeActionsOnSave
with "source.fixAll": "explicit"
(or true) imports that are unused in the cell they are imported in are incorrectly removed despite being used in following cells. settings.json
and pyproject.toml
values provided in collapsable.
Example
pip install jupyter polars
Create a notebook and add the following two cells
After performing a save with with the source.fixAll
set to true
or "explicit"
the import polars as pl
will be removed if the pl
name is not used in the same cell as the import. Meaning merging the cells into one will behave as expected but, two separate cells as seen in the image will incorrectly remove the import.
Details
Python: 3.10.10
Ruff: 0.1.13
OS: Darwin arm64 - Sonoma
Configs
settings.json
{ "notebook.formatOnSave.enabled": true, "notebook.codeActionsOnSave": { "source.fixAll": "explicit", // This line "source.organizeImports": "explicit" }, "[python]": { "editor.formatOnSave": true, "editor.defaultFormatter": "charliermarsh.ruff", "editor.codeActionsOnSave": { "source.fixAll": "explicit", "source.organizeImports": "explicit" } } }
pyproject.toml
[tool.ruff]
Exclude a variety of commonly ignored directories.
exclude = [ ".bzr", ".direnv", ".eggs", ".git", ".git-rewrite", ".hg", ".ipynb_checkpoints", ".mypy_cache", ".nox", ".pants.d", ".pyenv", ".pytest_cache", ".pytype", ".ruff_cache", ".svn", ".tox", ".venv", ".vscode", "pypackages", "_build", "buck-out", "build", "dist", "node_modules", "site-packages", "venv", ]
Same as Black.
line-length = 88 indent-width = 4
Assume Python 3.8
target-version = "py310"
[tool.ruff.lint]
Enable Pyflakes (F
) and a subset of the pycodestyle (E
) codes by default.
select = ["E4", "E7", "E9", "F"] ignore = []
Allow fix for all enabled rules (when --fix
) is provided.
fixable = ["ALL"] unfixable = []
Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(+|(+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
[tool.ruff.format]
Like Black, use double quotes for strings.
quote-style = "double"
Like Black, indent with spaces, rather than tabs.
indent-style = "space"
Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false
Like Black, automatically detect the appropriate line ending.
line-ending = "auto"