Skip to content

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

Description

@theelderbeever

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
{
    "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"

Activity

  1. charliermarsh commented on Jan 16, 2024

    @charliermarsh
    Member

    Can you try instead with:

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

    Notice the notebook. prefix.

  2. theelderbeever commented on Jan 16, 2024

    @theelderbeever
    Author

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

  3. charliermarsh commented on Jan 16, 2024

    @charliermarsh
    Member

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions