black icon indicating copy to clipboard operation
black copied to clipboard

Don't insert extra blank lines at the start of an indent

Open Rizhiy opened this issue 3 years ago • 3 comments

Describe the style change

If a function/class is defined at the start of a non-scoping indent (e.g. if, for) a blank line is inserted before the function. I would like this removed, since:

  • If an indent is scoping (e.g. def, class) no blank line is inserted and I would prefer if style was consistent.
  • I believe this is redundant since the indent already separates the function/class definition well enough.

Examples in the current Black style

# No blank lines
def f():
    def g():
        pass


class A:
    def g():
        pass


# Blank lines
for _ in range(10):

    def g():
        pass


if True:

    def g():
        pass

Desired style

# No blank lines
def f():
    def g():
        pass


class A:
    def g():
        pass


# Also, no blank lines
for _ in range(10):
    def g():
        pass


if True:
    def g():
        pass

Additional context

The issue came up when my team was developing a tool to remove blank lines at the start of indents, would be cool if black autocorrected that as well.

Rizhiy avatar May 27 '22 13:05 Rizhiy

Thanks for submitting! We've pretty much agreed that this is wanted, and it's already implemented in #3035 (which started as a fix to #902). I'll link this issue as a closing target there.

felix-hilden avatar May 30 '22 07:05 felix-hilden

@felix-hilden I just checked #3035 and it doesn't work for my case. Please note that my case specifically talks about function/class definition inside a block.

I believe there is another rule which enforces one blank line before function/class declaration and there is a conflict as a result.

Rizhiy avatar May 30 '22 14:05 Rizhiy

Right, I checked the code once more, sorry about that. The line in question I believe is this, which forces a newline if we are some blocks deep in the nesting for defs and classes.

I agree with you that after opening a block, a new line before defs and classes is pretty redundant. So after #3035 is merged, I'd be happy to include this one as well! Others?

felix-hilden avatar May 30 '22 14:05 felix-hilden

Duplicate of #450

JelleZijlstra avatar Apr 29 '23 01:04 JelleZijlstra