ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Allow newlines after block open (or not)

Open vaughnkoch opened this issue 1 year ago • 1 comments

Hi, I'm opening this issue per a suggestion from @MichaReiser, based on this closed issue: https://github.com/astral-sh/ruff/issues/8893.

The formatting question is whether Ruff should automatically remove a blank line after a block definition:

def some_func():

    some_more_code()

to

def some_func():
    some_more_code()

The alternative is "allow zero or one" newlines, i.e. not to remove the newline if it exists, but also not to automatically add one. This is the approach that Black chose in their 2024 preview style. Full Black discussion here: https://github.com/psf/black/issues/4043.

vaughnkoch avatar Jan 31 '24 17:01 vaughnkoch

Yes I can second this request as well. While investigating the transition to ruff, this inconsistency to black is causing many changes across our code base.

cbandera avatar Feb 23 '24 12:02 cbandera

Yes I can second this request as well. While investigating the transition to ruff, this inconsistency to black is causing many changes across our code base.

Thanks for reporting. Do you mainly mind that there are many differences or did you prefer black's formatting? Would you be able to share some examples where you preferred black's formatting over ruff's?

MichaReiser avatar Feb 23 '24 12:02 MichaReiser

Yes I can second this request as well. While investigating the transition to ruff, this inconsistency to black is causing many changes across our code base.

Thanks for reporting. Do you mainly mind that there are many differences or did you prefer black's formatting? Would you be able to share some examples where you preferred black's formatting over ruff's?

For me it's mainly about the diff that is created. Working in a big monorepo, it's always easier for us toolies to roll out changes if we don't need to touch the other devs code. Having read that ruff would be a "drop-in" replacement for black, my expectation was that I would see no diff at all.

Having said that and after looking at the known deviations I can't say that I don't agree with ruff's design decisions though...

cbandera avatar Feb 23 '24 12:02 cbandera

Hi, yes please add an option to preserve up to 1 empty line.

As empty lines used for separating/grouping lines, the first line(s) stay closer to block head but not the remaining parts feel like it's part of the block head (since empty line inside block content is inevitable), e.g.

block head
   part of block head

   block content
   block content
   block content

   block content
   block content

so I prefer to read as

block head

   block content
   block content
   block content

   block content
   block content

Personally I always insert an empty line at the beginning of the block, except 2 scenarios:

class B(A):

    def single_line(self):    # an empty line after `class B`, because single_line() is not closer to B than other methods
        return something    # Two exceptions that no empty line after `def instance_methods():`: 1. one liner, otherwises a few one lines consecutively will feel strange as there are equal space between block head and content and next block head and content

    def override_super(self):
        super().override_super()    # 2. `super()...` call (easier to tell whether non-one-liner class method called super or not? just personal preference probably)

        remaining_codes = ...

    def function(
        self, data: torch.Tensor | tuple[torch.Tensor, ...], other_argument: int
    ) -> torch.Tensor | tuple[torch.Tensor, ...]:
        something = do_something(data)    # An empty line above please, this line is part of method body, not head. borrowed from black issue comment

        return something

chicheng avatar Mar 27 '24 15:03 chicheng

Any updates on this? This is one the last remaining issues for ruff to finally replace black in my workflow.

alechouse97 avatar May 30 '24 19:05 alechouse97

Also checking in to say the same as above, I think this is our last remaining "issue" with ruff.

michaelhball avatar Jul 11 '24 09:07 michaelhball

Yes I can second this request as well. While investigating the transition to ruff, this inconsistency to black is causing many changes across our code base.

Agreed, this is what's blocking us from adopting Ruff. Too many diffs - not a "drop-in".

Looking forward to updates, thanks!

dcnadler avatar Aug 01 '24 20:08 dcnadler