Skip to content

Commit

Permalink
Allow empty lines at beginnings of more blocks
Browse files Browse the repository at this point in the history
Fixes psf#4043, fixes psf#619

These include nested functions and methods.

I think the nested function case quite clearly improves readability. I
think the method case improves consistency, adherence to PEP 8 and
resolves a point of contention.
  • Loading branch information
hauntsaninja committed Dec 28, 2023
1 parent db9c592 commit 2c1b0f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/black/lines.py
Expand Up @@ -745,7 +745,10 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
if self.previous_line.depth < current_line.depth and (
self.previous_line.is_class or self.previous_line.is_def
):
return 0, 0
if self.mode.is_pyi or not Preview.allow_empty_first_line_in_block:
return 0, 0
else:
return 1 if user_had_newline else 0, 0

comment_to_add_newlines: Optional[LinesBlock] = None
if (
Expand Down
1 change: 1 addition & 0 deletions tests/data/cases/class_blank_parentheses.py
Expand Up @@ -39,6 +39,7 @@ def test_func(self):


class ClassWithEmptyFunc(object):

def func_with_blank_parentheses():
return 5

Expand Down
21 changes: 20 additions & 1 deletion tests/data/cases/preview_allow_empty_first_line.py
Expand Up @@ -22,7 +22,7 @@ def foo():
Long comment here
"""
a = 123

if z:

for _ in range(100):
Expand Down Expand Up @@ -62,6 +62,15 @@ def method(self):

pass


def top_level(
a: int,
b: str,
) -> Whatever[Generic, Something]:

def nested(x: int) -> int:
pass

# output

def foo():
Expand Down Expand Up @@ -123,6 +132,16 @@ def quux():


class Cls:

def method(self):

pass


def top_level(
a: int,
b: str,
) -> Whatever[Generic, Something]:

def nested(x: int) -> int:
pass
7 changes: 4 additions & 3 deletions tests/data/cases/preview_form_feeds.py
Expand Up @@ -37,7 +37,7 @@
#

#

#

\
Expand Down Expand Up @@ -72,7 +72,7 @@
pass

pass

pass


Expand All @@ -95,7 +95,7 @@ class Baz:
def __init__(self):
pass


def something(self):
pass

Expand Down Expand Up @@ -203,6 +203,7 @@ def bar(a=1, b: bool = False):


class Baz:

def __init__(self):
pass

Expand Down

0 comments on commit 2c1b0f7

Please sign in to comment.