astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
1.06k stars 53 forks source link

Notebook imports are incorrectly removes imports with `"source.fixAll": "explicit"` #391

Closed theelderbeever closed 8 months ago

theelderbeever commented 8 months ago

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

image

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 ```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 ```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" ```
charliermarsh commented 8 months ago

Can you try instead with:

{
  "notebook.codeActionsOnSave": {
    "notebook.source.fixAll": "explicit"
    }
}

Notice the notebook. prefix.

theelderbeever commented 8 months ago

@charliermarsh A little embarrassed I missed that in the README... Thanks for the quick fix.

charliermarsh commented 8 months ago

@theelderbeever - No problem at all, I'm glad it worked! We added support for the notebook. versions very recently.