Skip to content

Commit fe33761

Browse files
authored
Allow empty lines at beginnings of more blocks (psf#4130)
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.
1 parent c359246 commit fe33761

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Format module docstrings the same as class and function docstrings (#4095)
1818
- Fix bug where spaces were not added around parenthesized walruses in subscripts,
1919
unlike other binary operators (#4109)
20+
- Address a missing case in the change to allow empty lines at the beginning of all
21+
blocks, except immediately before a docstring (#4130)
2022

2123
### Configuration
2224

src/black/lines.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,10 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
745745
if self.previous_line.depth < current_line.depth and (
746746
self.previous_line.is_class or self.previous_line.is_def
747747
):
748-
return 0, 0
748+
if self.mode.is_pyi or not Preview.allow_empty_first_line_in_block:
749+
return 0, 0
750+
else:
751+
return 1 if user_had_newline else 0, 0
749752

750753
comment_to_add_newlines: Optional[LinesBlock] = None
751754
if (

tests/data/cases/class_blank_parentheses.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_func(self):
3939

4040

4141
class ClassWithEmptyFunc(object):
42+
4243
def func_with_blank_parentheses():
4344
return 5
4445

tests/data/cases/preview_allow_empty_first_line.py

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def method(self):
6262

6363
pass
6464

65+
66+
def top_level(
67+
a: int,
68+
b: str,
69+
) -> Whatever[Generic, Something]:
70+
71+
def nested(x: int) -> int:
72+
pass
73+
6574
# output
6675

6776
def foo():
@@ -123,6 +132,16 @@ def quux():
123132

124133

125134
class Cls:
135+
126136
def method(self):
127137

128138
pass
139+
140+
141+
def top_level(
142+
a: int,
143+
b: str,
144+
) -> Whatever[Generic, Something]:
145+
146+
def nested(x: int) -> int:
147+
pass

tests/data/cases/preview_form_feeds.py

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def bar(a=1, b: bool = False):
203203

204204

205205
class Baz:
206+
206207
def __init__(self):
207208
pass
208209

0 commit comments

Comments
 (0)