cleanup-after-fix
KDruzhkin opened this issue · 5 comments
TL;DR
Some fixes, when applied, create conditions for other fixes.
On one hand, I understand that VSCode extension fixes one thing at a time (and this is by design). On the other hand, it is not fun to fix violations created by the tool.
I would like to have a configuration option for ruff to clean up after itself:
- if the programmer explicitly applies rule A,
- and if rule A triggers (previously silent) rule B,
- and if rule B is safe to apply,
- then apply rule B as well.
An example
Recently I went through a large code base, adding from __future__ import annotations
where it missed. This simple change started a cascade of other changes.
This code is flagged with TCH002 ("Move numpy.typing
into a type-checking block"):
from __future__ import annotations
from typing import TypeAlias
import numpy.typing as t
ID: TypeAlias = int
def foo(x: t.NDArray) -> int:
return len(x)
I obediently click Ctrl_+.
and select a quick fix.
Now, this code is flagged with I001 ("Import block is unsorted"), because TypeAlias, TYPE_CHECKING
are in the wrong order, and there is an extra newline before if TYPE_CHECKING
:
from __future__ import annotations
from typing import TypeAlias, TYPE_CHECKING
if TYPE_CHECKING:
import numpy.typing as t
ID: TypeAlias = int
def foo(x: t.NDArray) -> int:
return len(x)
Clicking again, I get the final version:
from __future__ import annotations
from typing import TYPE_CHECKING, TypeAlias
if TYPE_CHECKING:
import numpy.typing as t
ID: TypeAlias = int
def foo(x: t.NDArray) -> int:
return len(x)
Sometimes the chain is longer than two steps, but I cannot reproduce it now.
For reference, we do fix until convergence if you run Fix All
.
For reference, we do fix until convergence if you run
Fix All
.
Yes, I was in a hurry when I wrote astral-sh/ruff#10954, but now I see.
Perhaps, I should rename this issue to cleanup-after-fix?
Doesn't Fix All
code action do exactly that? It'll try to apply the fixes until there are none. Or, am I misunderstanding on what you're trying to achieve?
Doesn't
Fix All
code action do exactly that? It'll try to apply the fixes until there are none. Or, am I misunderstanding on what you're trying to achieve?
Not exactly. Suppose I have lots of ugly code below which I do not want to touch just yet. But I am ready to fix the imports. In this situation I cannot run Fix All
.
I see. Thanks for sharing that. I think astral-sh/ruff#4361 could potentially solve this at least when working from the command-line. I'm not sure what it would look like in the editor.