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

(🎁) preserve node line of noqa comments when formatting #12901

Open
KotlinIsland opened this issue Aug 15, 2024 · 5 comments
Open

(🎁) preserve node line of noqa comments when formatting #12901

KotlinIsland opened this issue Aug 15, 2024 · 5 comments
Labels
incompatibility Incompatibility between different Ruff tools or rules (formatter, isort, linter) suppression Related to supression of violations e.g. noqa wish Not on the current roadmap; maybe in the future

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Aug 15, 2024

dict(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1)  # noqa: C408

after format (fail)

dict(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1
)  # noqa: C408

ruff is doing the checking of this noqa, ruff is also the one doing the formating, so it would seem logical that ruff use the check information to keep the noqa on the right line

dict(  # noqa: C408
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1
)

Additionally

this isn't exclusive to format as changes from check can also conflict, see #12179

related:

@MichaReiser
Copy link
Member

I understand why this is desired, but it has quite far-reaching implications, and it would significantly slow down formatting because formatting would require running the linter, a trade-off that I think isn't worth the occasional move of a noqa comment.

Long-term, this does not just apply to running the linter but also type-checking and there are suppression comments that ruff doesn't even understand. So the problem doesn't go away, it might just happen less often (which would already be nice).

To me the most likely solution is redesigning suppression comments so that it is known what target they suppress by being more explicit about where they can be placed. E.g. only allow own-line suppression comments, maybe even on a statement level.

@MichaReiser MichaReiser added suppression Related to supression of violations e.g. noqa incompatibility Incompatibility between different Ruff tools or rules (formatter, isort, linter) wish Not on the current roadmap; maybe in the future labels Aug 15, 2024
@KotlinIsland
Copy link
Contributor Author

99% of the time I want to both format and check, currently it's two separate steps, and I would presume that would make it take longer

Personally I would love to trade speed for more correctness, but know not everyone thinks the same

@filips123
Copy link

I have the same problem with with mypy's type: ignore comments.

For example, the following line:

    def parse_document(self, document: DocumentInfo, stream: BytesIO, effective: datetime.date, span: Span) -> None:  # type: ignore[override]
        ...

Gets reformatted as:

    def parse_document(
        self, document: DocumentInfo, stream: BytesIO, effective: datetime.date, span: Span
    ) -> None:  # type: ignore[override]
        ...

Which makes mypy unable to detect the ignore comment, as it has been moved to the incorrect place.

Black prevents splitting lines that contain type: ignore comments. Maybe this could be added as an option to Ruff formatter?

@PamelaM
Copy link

PamelaM commented Sep 12, 2024

Another example I've run into:
Here's my one_line.py file before ruff:

from implementations.endpoints.name_of_some_module import very_very_long_name_of_some_function # noqa: F401
$ ruff check one_line.py
All checks passed!
$ ruff format one_line.py
1 file reformatted
$ ruff check one_line.py
... cut details ...
Found 1 error.
[*] 1 fixable with the `--fix` option.

Reformatted file:

from implementations.endpoints.name_of_some_module import (
    very_very_long_name_of_some_function,
) # noqa: F401

Workaround:

# fmt: off
from implementations.endpoints.name_of_some_module import very_very_long_name_of_some_function # noqa: F401
# fmt: on

@KotlinIsland
Copy link
Contributor Author

@PamelaM that looks very similar to #12179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incompatibility Incompatibility between different Ruff tools or rules (formatter, isort, linter) suppression Related to supression of violations e.g. noqa wish Not on the current roadmap; maybe in the future
Projects
None yet
Development

No branches or pull requests

4 participants