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
@dylwil3 dylwil3 force-pushed the fmt-skip-one-liner branch 3 times, most recently from b5cde38 to 417dd01 Compare November 17, 2025 15:04
@dylwil3 dylwil3 marked this pull request as ready for review November 17, 2025 16:48
@dylwil3 dylwil3 requested a review from MichaReiser November 17, 2025 16:48
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.

Nice

@dylwil3 dylwil3 merged commit 62343a1 into astral-sh:main Nov 18, 2025
36 checks passed
@dylwil3 dylwil3 deleted the fmt-skip-one-liner branch November 18, 2025 18:02
dylwil3 added a commit that referenced this pull request Jan 10, 2026
)

This PR adjusts the logic for skipping formatting so that a `fmt: skip`
can affect multiple statements if they lie on the same line.

Specifically, a `fmt: skip` comment will now suppress all the statements
in the suite in which it appears whose range intersects the line
containing the skip directive. For example:

```python
x=[
'1'
];x=2 # fmt: skip
```

remains unchanged after formatting.

(Note that compound statements are somewhat special and were handled in
a previous PR - see #20633).


Closes #17331 and #11430.

Simplest to review commit by commit - the key diffs of interest are the
commit introducing the core logic, and the diff between the snapshots
introduced in the last commit (compared to the second commit).

# Implementation

On `main` we format a suite of statements by iterating through them. If
we meet a statement with a leading or trailing (own-line)`fmt: off`
comment, then we suppress formatting until we meet a `fmt: on` comment.
Otherwise we format the statement using its own formatting rule.

How are `fmt: skip` comments handled then? They are handled internally
to the formatting of each statement. Specifically, calling `.fmt` on a
statement node will first check to see if there is a trailing,
end-of-line `fmt: skip` (or `fmt: off`/`yapf: off`), and if so then
write the node with suppressed formatting.

In this PR we move the responsibility for handling `fmt: skip` into the
formatting logic of the suite itself. This is done as follows:

- Before beginning to format the suite, we do a pass through the
statements and collect the data of ranges with skipped formatting. More
specifically, we create a map with key given by the _first_ skipped
statement in a block and value a pair consisting of the _last_ skipped
statement and the _range_ to write verbatim.
- We iterate as before, but if we meet a statement that is a key in the
map constructed above, we pause to write the associated range verbatim.
We then advance the iterator to the last statement in the block and
proceed as before.

## Addendum on range formatting

We also had to make some changes to range formatting in order to support
this new behavior. For example, we want to make sure that

```python
<RANGE_START>x=1<RANGE_END>;x=2 # fmt: skip
```

formats verbatim, rather than becoming 

```python
x = 1;x=2 # fmt: skip
```

Recall that range formatting proceeds in two steps:
1. Find the smallest enclosing node containing the range AND that has
enough info to format the range (so it may be larger than you think,
e.g. a docstring has enclosing node given by the suite, not the string
itself.)
2. Carve out the formatted range from the result of formatting that
enclosing node.

We had to modify (1), since the suite knows how to format skipped nodes,
but nodes may not "know" they are skipped. To do this we altered the
`visit_body` bethod of the `FindEnclosingNode` visitor: now we iterate
through the statements and check for skipped ranges intersecting the
format range. If we find them, we return without descending. The result
is to consider the statement containing the suite as the enclosing node
in this case.
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