From 18e711f7b46f76aa5ada2f54f639866631f83122 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 20 Nov 2023 21:04:50 -0800 Subject: [PATCH 1/5] Allow empty lines at beginning of blocks (again) --- CHANGES.md | 3 ++- src/black/lines.py | 10 +++------- src/black/mode.py | 2 +- ...cial_cases.py => preview_allow_empty_first_line.py} | 9 +++++++++ tests/data/cases/preview_form_feeds.py | 1 + 5 files changed, 16 insertions(+), 9 deletions(-) rename tests/data/cases/{preview_allow_empty_first_line_in_special_cases.py => preview_allow_empty_first_line.py} (94%) diff --git a/CHANGES.md b/CHANGES.md index 18bab5131e6..41a4f86c19a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,9 +13,10 @@ ### Preview style - Standalone form feed characters at the module level are no longer removed (#4021) - - Additional cases of immediately nested tuples, lists, and dictionaries are now indented less (#4012) +- Allow empty lines at the beginning of all blocks, except immediately before a + docstring (#4060) ### Configuration diff --git a/src/black/lines.py b/src/black/lines.py index ec6145ff848..cf89d8b6b50 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -685,17 +685,13 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: return before, 1 is_empty_first_line_ok = ( - Preview.allow_empty_first_line_before_new_block_or_comment + Preview.allow_empty_first_line_in_block in current_line.mode and ( - # If it's a standalone comment - current_line.leaves[0].type == STANDALONE_COMMENT - # If it opens a new block - or current_line.opens_block + not is_docstring(current_line.leaves[0]) # If it's a triple quote comment (but not at the start of a funcdef) or ( - is_docstring(current_line.leaves[0]) - and self.previous_line + self.previous_line and self.previous_line.leaves[0] and self.previous_line.leaves[0].parent and not is_funcdef(self.previous_line.leaves[0].parent) diff --git a/src/black/mode.py b/src/black/mode.py index 04038f49627..9df19618363 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -191,7 +191,7 @@ class Preview(Enum): accept_raw_docstrings = auto() fix_power_op_line_length = auto() hug_parens_with_braces_and_square_brackets = auto() - allow_empty_first_line_before_new_block_or_comment = auto() + allow_empty_first_line_in_block = auto() single_line_format_skip_with_multiple_comments = auto() long_case_block_line_splitting = auto() allow_form_feeds = auto() diff --git a/tests/data/cases/preview_allow_empty_first_line_in_special_cases.py b/tests/data/cases/preview_allow_empty_first_line.py similarity index 94% rename from tests/data/cases/preview_allow_empty_first_line_in_special_cases.py rename to tests/data/cases/preview_allow_empty_first_line.py index 96c1433c110..600e737449e 100644 --- a/tests/data/cases/preview_allow_empty_first_line_in_special_cases.py +++ b/tests/data/cases/preview_allow_empty_first_line.py @@ -51,6 +51,10 @@ def baz(): if x: a = 123 +def quux(): + + new_line = here + # output def foo(): @@ -104,3 +108,8 @@ def baz(): # OK if x: a = 123 + + +def quux(): + + new_line = here diff --git a/tests/data/cases/preview_form_feeds.py b/tests/data/cases/preview_form_feeds.py index 2d8653a1f04..c236f177a95 100644 --- a/tests/data/cases/preview_form_feeds.py +++ b/tests/data/cases/preview_form_feeds.py @@ -198,6 +198,7 @@ def foo(): # form feeds are prohibited inside blocks, or on a line with nonwhitespace def bar(a=1, b: bool = False): + pass From d7537030053edf07a2ed2fd420544bfa1520db5b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 20 Nov 2023 21:10:50 -0800 Subject: [PATCH 2/5] reformat --- src/black/lines.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index cf89d8b6b50..8895f5b2d1b 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -684,18 +684,14 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: return 0, 1 return before, 1 - is_empty_first_line_ok = ( - Preview.allow_empty_first_line_in_block - in current_line.mode - and ( - not is_docstring(current_line.leaves[0]) - # If it's a triple quote comment (but not at the start of a funcdef) - or ( - self.previous_line - and self.previous_line.leaves[0] - and self.previous_line.leaves[0].parent - and not is_funcdef(self.previous_line.leaves[0].parent) - ) + is_empty_first_line_ok = Preview.allow_empty_first_line_in_block in current_line.mode and ( + not is_docstring(current_line.leaves[0]) + # If it's a triple quote comment (but not at the start of a funcdef) + or ( + self.previous_line + and self.previous_line.leaves[0] + and self.previous_line.leaves[0].parent + and not is_funcdef(self.previous_line.leaves[0].parent) ) ) From 129349ce664e514c70fa93023332277a5fd27c95 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 20 Nov 2023 21:11:44 -0800 Subject: [PATCH 3/5] update comments --- src/black/lines.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 8895f5b2d1b..37615c473fa 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -684,14 +684,17 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: return 0, 1 return before, 1 - is_empty_first_line_ok = Preview.allow_empty_first_line_in_block in current_line.mode and ( - not is_docstring(current_line.leaves[0]) - # If it's a triple quote comment (but not at the start of a funcdef) - or ( - self.previous_line - and self.previous_line.leaves[0] - and self.previous_line.leaves[0].parent - and not is_funcdef(self.previous_line.leaves[0].parent) + # In preview mode, always allow blank lines, except right before a function docstring + is_empty_first_line_ok = ( + Preview.allow_empty_first_line_in_block in current_line.mode + and ( + not is_docstring(current_line.leaves[0]) + or ( + self.previous_line + and self.previous_line.leaves[0] + and self.previous_line.leaves[0].parent + and not is_funcdef(self.previous_line.leaves[0].parent) + ) ) ) From 6ff017b62ae90c81a2fe7590b81df3e227d25180 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 22 Nov 2023 17:27:41 -0800 Subject: [PATCH 4/5] lint --- src/black/lines.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/black/lines.py b/src/black/lines.py index 37615c473fa..0f24192447d 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -684,7 +684,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: return 0, 1 return before, 1 - # In preview mode, always allow blank lines, except right before a function docstring + # In preview mode, always allow blank lines, except right before a function + # docstring is_empty_first_line_ok = ( Preview.allow_empty_first_line_in_block in current_line.mode and ( From dcff0928dcf8be1f60ff7f153d16888ee3ed32d4 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 9 Dec 2023 17:50:49 -0800 Subject: [PATCH 5/5] add a case --- tests/data/cases/preview_allow_empty_first_line.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/data/cases/preview_allow_empty_first_line.py b/tests/data/cases/preview_allow_empty_first_line.py index 600e737449e..3e14fa15250 100644 --- a/tests/data/cases/preview_allow_empty_first_line.py +++ b/tests/data/cases/preview_allow_empty_first_line.py @@ -55,6 +55,13 @@ def quux(): new_line = here + +class Cls: + + def method(self): + + pass + # output def foo(): @@ -113,3 +120,9 @@ def baz(): def quux(): new_line = here + + +class Cls: + def method(self): + + pass