Skip to content

Commit 61b529b

Browse files
Allow empty lines at beginning of blocks (again) (#4060)
1 parent e7e122e commit 61b529b

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Standalone form feed characters at the module level are no longer removed (#4021)
1818
- Additional cases of immediately nested tuples, lists, and dictionaries are now
1919
indented less (#4012)
20+
- Allow empty lines at the beginning of all blocks, except immediately before a
21+
docstring (#4060)
2022
- Fix crash in preview mode when using a short `--line-length` (#4086)
2123

2224
### Configuration

src/black/lines.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -689,18 +689,14 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
689689
return 0, 1
690690
return before, 1
691691

692+
# In preview mode, always allow blank lines, except right before a function
693+
# docstring
692694
is_empty_first_line_ok = (
693-
Preview.allow_empty_first_line_before_new_block_or_comment
694-
in current_line.mode
695+
Preview.allow_empty_first_line_in_block in current_line.mode
695696
and (
696-
# If it's a standalone comment
697-
current_line.leaves[0].type == STANDALONE_COMMENT
698-
# If it opens a new block
699-
or current_line.opens_block
700-
# If it's a triple quote comment (but not at the start of a funcdef)
697+
not is_docstring(current_line.leaves[0])
701698
or (
702-
is_docstring(current_line.leaves[0])
703-
and self.previous_line
699+
self.previous_line
704700
and self.previous_line.leaves[0]
705701
and self.previous_line.leaves[0].parent
706702
and not is_funcdef(self.previous_line.leaves[0].parent)

src/black/mode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Preview(Enum):
191191
accept_raw_docstrings = auto()
192192
fix_power_op_line_length = auto()
193193
hug_parens_with_braces_and_square_brackets = auto()
194-
allow_empty_first_line_before_new_block_or_comment = auto()
194+
allow_empty_first_line_in_block = auto()
195195
single_line_format_skip_with_multiple_comments = auto()
196196
long_case_block_line_splitting = auto()
197197
allow_form_feeds = auto()

tests/data/cases/preview_allow_empty_first_line_in_special_cases.py tests/data/cases/preview_allow_empty_first_line.py

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ def baz():
5151
if x:
5252
a = 123
5353

54+
def quux():
55+
56+
new_line = here
57+
58+
59+
class Cls:
60+
61+
def method(self):
62+
63+
pass
64+
5465
# output
5566

5667
def foo():
@@ -104,3 +115,14 @@ def baz():
104115
# OK
105116
if x:
106117
a = 123
118+
119+
120+
def quux():
121+
122+
new_line = here
123+
124+
125+
class Cls:
126+
def method(self):
127+
128+
pass

tests/data/cases/preview_form_feeds.py

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def foo():
198198

199199
# form feeds are prohibited inside blocks, or on a line with nonwhitespace
200200
def bar(a=1, b: bool = False):
201+
201202
pass
202203

203204

0 commit comments

Comments
 (0)