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

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

Open
ngoctnq opened this issue May 15, 2024 · 3 comments
Open

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

ngoctnq opened this issue May 15, 2024 · 3 comments
Labels
formatter Related to the formatter suppression Related to supression of violations e.g. noqa

Comments

@ngoctnq
Copy link

ngoctnq commented May 15, 2024

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

@dhruvmanila
Copy link
Member

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 dhruvmanila added suppression Related to supression of violations e.g. noqa formatter Related to the formatter labels May 15, 2024
@konstin
Copy link
Member

konstin commented May 15, 2024

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

@MichaReiser
Copy link
Member

MichaReiser commented May 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter suppression Related to supression of violations e.g. noqa
Projects
None yet
Development

No branches or pull requests

4 participants