ruff icon indicating copy to clipboard operation
ruff copied to clipboard

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

Open randolf-scholz opened this issue 1 year ago • 1 comments

if True: x = 0  # fmt: skip

def foo(self, lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, do): ...  # fmt: skip

Gets reformatted to

if True:
    x = 0  # fmt: skip

def foo(
    self, lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, do
): ...  # fmt: skip

Which is because fmt: skip only applies to the preceding statement. However, it would be useful to apply the fmt: skip to the whole compound statement in this context. For instance, when formatting @overload decorated functions.

randolf-scholz avatar Apr 30 '24 16:04 randolf-scholz

Yeah that makes sense to me. I'm not entirely sure how to implement this yet. I see two approaches:

  1. Add special handling for fmt: skip and fmt: off (and I think yapf:disable) comments in placement.rs that come at the end of a clause header. Instead of associating them with the preceding statement, associate them with the clause header (but only if both are on the same line).
  2. Pass the comments of the first simple statement to FormatClauseHeader and make the decision there.

The good news, the unused formatter suppression comment lint rule seems to already accept this.

MichaReiser avatar May 01 '24 07:05 MichaReiser

Hi @MichaReiser I did a first pass on this issue. Not sure if my approach is what we were looking for but I'd be happy to change direction. Let me know 👍 https://github.com/astral-sh/ruff/pull/13487

bnkc avatar Sep 23 '24 20:09 bnkc