astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.

Home Page:https://docs.astral.sh/ruff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`# noqa: E702 # fmt: skip` still formats the line of code

ngoctnq opened this issue · comments

Input code:

print(); print() # noqa # fmt: skip

After formatting:

print()
print()  # noqa # fmt: skip

# fmt: on/off works fine though, but I want to have just one line of short boilerplate code.

I'm using the Ruff VS Code extension, which is said to ship with ruff==0.4.1

I think it's because there are two statements on the same line and fmt: skip is being applied only for the last statement. This can be seen by adding a single-quoted string (https://play.ruff.rs/edf37109-eb34-4b5c-9867-dd5587bef085), notice that the formatter only changes the quote for the first print call and not the second one. I don't think RUF028 will indicate here because the suppression comment is actually being used by the formatter.

I'm not exactly sure if this is the intended behavior, @konstin any thoughts here? @MichaReiser will be able to provide more context here but he's currently on PTO until the end of next week. I can possibly look into it to confirm whether this is the intended behavior.

Iirc the end-of-line format off comment applies to the statement preceding it, and the two prints are two separate statements, but micha will know better

Oh, that's interesting and not something I have thought of when implementing suppression comments.

# fmt: skip comments suppress formatting for a preceding statement, case header, decorator, function definition, or class definition:

Today's behavior matches our documentation. However, Black doesn't format simple statements that end with a fmt: skip comment. So it is at least a Black incompatibility (and possibly not flagged by the ruff lint rule).

I'm open to changing our implementation to support this use case if it doesn't add too much complexity. I otherwise recommend documenting the deviation because it's possible to suppress the example using fmt: off (although it is slightly more verbose).

# fmt: off
print(); print() # noqa 
# fmt: on

print( )