astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
946 stars 45 forks source link

cleanup-after-fix #451

Open KDruzhkin opened 2 months ago

KDruzhkin commented 2 months ago

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:

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.

charliermarsh commented 2 months ago

For reference, we do fix until convergence if you run Fix All.

KDruzhkin commented 2 months ago

For reference, we do fix until convergence if you run Fix All.

Yes, I was in a hurry when I wrote https://github.com/astral-sh/ruff/issues/10954, but now I see.

Perhaps, I should rename this issue to cleanup-after-fix?

dhruvmanila commented 2 months ago

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?

KDruzhkin commented 2 months ago

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.

dhruvmanila commented 2 months ago

I see. Thanks for sharing that. I think https://github.com/astral-sh/ruff/issues/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.