ruff icon indicating copy to clipboard operation
ruff copied to clipboard

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

Open ngoctnq opened this issue 1 year ago • 2 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

ngoctnq avatar May 15 '24 07:05 ngoctnq

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.

dhruvmanila avatar May 15 '24 08:05 dhruvmanila

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

konstin avatar May 15 '24 11:05 konstin

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( )

MichaReiser avatar May 27 '24 08:05 MichaReiser

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( )

yep. basically, I banged my head a few hours to figure this out. And, I finally just changed to Black formatter again.

Black doesn't format the line which has more than one statement and tagged with "# fmt: skip"

Gatsby-Lee avatar Jun 18 '24 07:06 Gatsby-Lee

@ngoctnq

thank you for writing this issue.

Gatsby-Lee avatar Jun 18 '24 07:06 Gatsby-Lee

Related: #11216.

Peiffap avatar Jun 21 '24 23:06 Peiffap