From 2fd9d8b339e1e2e1b93956c6d68b2b358b3fc29d Mon Sep 17 00:00:00 2001 From: Jonathan Berthias Date: Fri, 19 May 2023 01:57:17 +0200 Subject: [PATCH 1/1] Remove blank lines before class docstring (#3692) --- CHANGES.md | 1 + src/black/lines.py | 2 + src/black/mode.py | 1 + .../preview/no_blank_line_before_docstring.py | 58 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 tests/data/preview/no_blank_line_before_docstring.py diff --git a/CHANGES.md b/CHANGES.md index a1bc7d5..6a9923f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Implicitly concatenated strings used as function args are no longer wrapped inside parentheses (#3640) +- Remove blank lines between a class definition and its docstring (#3692) ### Configuration diff --git a/src/black/lines.py b/src/black/lines.py index 9d33bfa..daf0444 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -634,6 +634,8 @@ class EmptyLineTracker: and self.previous_line.is_class and current_line.is_triple_quoted_string ): + if Preview.no_blank_line_before_class_docstring in current_line.mode: + return 0, 1 return before, 1 if self.previous_line and self.previous_line.opens_block: diff --git a/src/black/mode.py b/src/black/mode.py index 3e37a58..a5841ed 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -158,6 +158,7 @@ class Preview(Enum): hex_codes_in_unicode_sequences = auto() improved_async_statements_handling = auto() multiline_string_handling = auto() + no_blank_line_before_class_docstring = auto() prefer_splitting_right_hand_side_of_assignments = auto() # NOTE: string_processing requires wrap_long_dict_values_in_parens # for https://github.com/psf/black/issues/3117 to be fixed. diff --git a/tests/data/preview/no_blank_line_before_docstring.py b/tests/data/preview/no_blank_line_before_docstring.py new file mode 100644 index 0000000..a37362d --- /dev/null +++ b/tests/data/preview/no_blank_line_before_docstring.py @@ -0,0 +1,58 @@ +def line_before_docstring(): + + """Please move me up""" + + +class LineBeforeDocstring: + + """Please move me up""" + + +class EvenIfThereIsAMethodAfter: + + """I'm the docstring""" + def method(self): + pass + + +class TwoLinesBeforeDocstring: + + + """I want to be treated the same as if I were closer""" + + +class MultilineDocstringsAsWell: + + """I'm so far + + and on so many lines... + """ + + +# output + + +def line_before_docstring(): + """Please move me up""" + + +class LineBeforeDocstring: + """Please move me up""" + + +class EvenIfThereIsAMethodAfter: + """I'm the docstring""" + + def method(self): + pass + + +class TwoLinesBeforeDocstring: + """I want to be treated the same as if I were closer""" + + +class MultilineDocstringsAsWell: + """I'm so far + + and on so many lines... + """ -- 2.39.5