Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty lines at beginnings of more blocks #4130

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Format module docstrings the same as class and function docstrings (#4095)
- Fix bug where spaces were not added around parenthesized walruses in subscripts,
unlike other binary operators (#4109)
- Address a missing case in the change to allow empty lines at the beginning of all
blocks, except immediately before a docstring (#4130)

### Configuration

Expand Down
5 changes: 4 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_func(self):


class ClassWithEmptyFunc(object):

def func_with_blank_parentheses():
return 5

Expand Down
19 changes: 19 additions & 0 deletions tests/data/cases/preview_allow_empty_first_line.py
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions tests/data/cases/preview_form_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def bar(a=1, b: bool = False):


class Baz:

def __init__(self):
pass

Expand Down
Loading