Skip to content

Conversation

@dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Sep 29, 2025

Closes #11216

Essentially the approach is to implement Format for a new struct FormatClause which is just a clause header and its body. We then have the information we need to see whether there is a skip suppression comment on the last child in the body and it all fits on one line.

The ruff test fixture (credit: mostly created by Claude) contains several lines that seem to crash black, see:

and another line containing a deviation that I think is unintentional for black, see:

However, it's not clear whether we want the same behavior here? In this PR we have:

if True: print("this"); print("that") # fmt: skip

unchanged, but we do add a newline between the statements here (unlike Black):

print("this"); print("that") # fmt: skip

(see #11430 and #17331 .)

The other deviation from black is that we format the placement of the skip comment itself.

For review, it is simplest to go commit by commit.

@dylwil3 dylwil3 added bug Something isn't working formatter Related to the formatter preview Related to preview mode features labels Sep 29, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 29, 2025

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@dylwil3 dylwil3 force-pushed the fmt-skip-one-liner branch 3 times, most recently from c695437 to aa2fd23 Compare October 6, 2025 20:34
@dylwil3 dylwil3 marked this pull request as ready for review October 6, 2025 20:44
@dylwil3 dylwil3 requested a review from MichaReiser as a code owner October 6, 2025 20:44
@dylwil3 dylwil3 mentioned this pull request Oct 6, 2025
31 tasks
@MichaReiser
Copy link
Member

MichaReiser commented Oct 7, 2025

unchanged, but we do add a newline between the statements here (unlike Black):

print("this"); print("that") # fmt: skip

This seems unrelated to this PR as it is a pre-existing issue, right?

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great :) I suggest adding a few more tests with different comment placements. Just to make sure we handle them correctly.

Ok(())
}),
body,
SuiteKind::Class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we derive the SuiteKind from the ClauseHeader to reduce the number of arguments?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so because I think we have to be told whether we're the last suite in the statement (but I think that's the only missing info)

@ntBre ntBre mentioned this pull request Oct 13, 2025
@dylwil3 dylwil3 force-pushed the fmt-skip-one-liner branch from aa2fd23 to 00d2823 Compare October 20, 2025 19:59
@dylwil3
Copy link
Collaborator Author

dylwil3 commented Oct 24, 2025

The CI failure here https://github.com/astral-sh/ruff/actions/runs/18664070057/job/53211233008?pr=20633 was caused by this PR uncovering a pre-existing bug. See #21065 and #21067 . Will rebase once that PR is merged and then continue working on this!

@dylwil3 dylwil3 force-pushed the fmt-skip-one-liner branch from 813abca to 6e1bb22 Compare October 27, 2025 15:22
@dylwil3 dylwil3 marked this pull request as draft October 27, 2025 16:19
@dylwil3
Copy link
Collaborator Author

dylwil3 commented Oct 27, 2025

Cut down a comment-related hydra head and more grew in its place. This snippet causes a panic because the indented comment is not marked as formatted. Annoyingly it is attached to bar() (because there is no orelse node).

for x in it: foo()
    # comment
else: bar() # fmt: skip

Converting to draft while I sort it out 😄

@dylwil3 dylwil3 removed the preview Related to preview mode features label Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH] Let fmt: skip apply to compound statements, if they share a line.

2 participants