Skip to content

E302 and E305 cause the linter and formatter to make conflicting changes in notebooks #10228

@vancromy

Description

@vancromy

The new rules introduced in ruff v0.2.2 via #9266 are causing the ruff linter to make conflicting changes in notebook cells that the formatter is then trying to fix.

Here is an example notebook which reproduces the bug:
image

ruff config:

[tool.ruff]
extend-include = ["*.ipynb"]

[tool.ruff.lint]
select = [
    "E",
]

If you run the commands in this succession and look at the diffs you will notice that the linter adds two lines at the end of:

  1. Cell 3 (the one just printing out the variable some_computation). This is rule E302 in action
  2. Cell 4 (after the function definition). This is rule E305 in action.

The commands I ran:

  1. ruff check --force-exclude --preview --fix test.ipynb
  2. ruff format --force-exclude --preview test.ipynb

(The --force-exclude is because I discovered this bug through pre-commit and I was trying to mimic the pre-commit behaviour)

This behaviour happens from v0.2.2 upwards (I've also tested v0.3.0) as long as the preview flag is on.

Let me know if the MRE needs more explanation.

Metadata

Metadata

Assignees

Labels

notebookRelated to (Jupyter) notebooksruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions