Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup-after-fix #451

Open
KDruzhkin opened this issue Apr 16, 2024 · 5 comments
Open

cleanup-after-fix #451

KDruzhkin opened this issue Apr 16, 2024 · 5 comments

Comments

@KDruzhkin
Copy link

KDruzhkin commented Apr 16, 2024

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.

@charliermarsh
Copy link
Member

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

@KDruzhkin
Copy link
Author

KDruzhkin commented Apr 16, 2024

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?

@KDruzhkin KDruzhkin changed the title fix-until-convergence cleanup-after-fix Apr 16, 2024
@dhruvmanila
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants