-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Respect fmt: skip for compound statements on single line
#20633
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
Conversation
|
c695437 to
aa2fd23
Compare
This seems unrelated to this PR as it is a pre-existing issue, right? |
MichaReiser
left a comment
There was a problem hiding this 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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__compound_one_liners.py.snap
Outdated
Show resolved
Hide resolved
crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__compound_one_liners.py.snap
Show resolved
Hide resolved
crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__compound_one_liners.py.snap
Show resolved
Hide resolved
aa2fd23 to
00d2823
Compare
|
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! |
813abca to
6e1bb22
Compare
|
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 for x in it: foo()
# comment
else: bar() # fmt: skipConverting to draft while I sort it out 😄 |
b5cde38 to
417dd01
Compare
417dd01 to
fd13d35
Compare
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
) 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.
Closes #11216
Essentially the approach is to implement
Formatfor a new structFormatClausewhich 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
rufftest fixture (credit: mostly created by Claude) contains several lines that seem to crashblack, see:fix_fmt_skip_in_one_linersfor certain compound statements psf/black#4782fix_fmt_skip_in_one_linersfor function with annotated parameter and# fmt: skipon one line psf/black#4781and another line containing a deviation that I think is unintentional for
black, see:fix_fmt_skip_in_one_linersdoes not work for semi-colon separated statements in body of compound statement psf/black#4783However, it's not clear whether we want the same behavior here? In this PR we have:
unchanged, but we do add a newline between the statements here (unlike Black):
(see #11430 and #17331 .)
The other deviation from
blackis that we format the placement of the skip comment itself.For review, it is simplest to go commit by commit.