Non-idempotent comment movement for ((x) # comment here ...) with parens and 3+ lines #16151
Labels
bug
Something isn't working
formatter
Related to the formatter
great writeup
A wonderful example of a quality contribution
Description
Context
We have some code along the lines of:
Switching from black to
ruff format
results in the comment moving:ruff format
invocationsReproducer
Formatting this code requires multiple invocations of
ruff format
to be "stable"/reach a fixed point:Input:
Output of formatting 1 (change: comment is shifted to next line):
Output of formatting 2 (change: comment is unindented):
Playground links:
Expected behaviour
ruff format
should be idempotent: the output of formatting should not have any changes if formatted again.That is, the first format call (step 1 -> 2) should give the final result with the comment in its final position (unindented), and the second (step 2 -> 3) should not change the code.
Alternatively, the formatting for this circumstance changed to sidestep the issue. It's seems weird to me that this comment is being wrapped. It moving the comment to the next line also problems if the comment is a
# type: ...
or# noqa
pragma.See also "Ablations".
Ablations
This stops reproducing with these variations of the Input (1):
Only one extra line after the comment (no change: comment remains where it is)
No parens around
x
(becomesx.y().z() # a
)Comment on a different line (no change):
Metadata
Ruff version: 0.9.6
Settings (default play.ruff.rs ones):
Search keywords: idempotent, idempotency, reformat, repeated invocations, fixed point, stable
Thanks for ruff!
The text was updated successfully, but these errors were encountered: