From 61d8400e42c471baed2ab4a122a195b03a09a1bf Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Mon, 6 Nov 2023 12:07:21 -0800 Subject: [PATCH] Keep requiring two empty lines between module-level docstring and first function or class definition. Fixes #4027. --- CHANGES.md | 2 ++ src/black/lines.py | 1 + .../data/cases/module_docstring_followed_by_class.py | 11 +++++++++++ .../cases/module_docstring_followed_by_function.py | 11 +++++++++++ 4 files changed, 25 insertions(+) create mode 100644 tests/data/cases/module_docstring_followed_by_class.py create mode 100644 tests/data/cases/module_docstring_followed_by_function.py diff --git a/CHANGES.md b/CHANGES.md index 97084a2bfc1..a68f87bfc12 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,8 @@ indented less (#3964) - Multiline list and dict unpacking as the sole argument to a function is now also indented less (#3992) +- Keep requiring two empty lines between module-level docstring and first function or + class definition. (#4028) ### Configuration diff --git a/src/black/lines.py b/src/black/lines.py index a73c429e3d9..23c1a93d3d4 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -578,6 +578,7 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock: and self.previous_block.previous_block is None and len(self.previous_block.original_line.leaves) == 1 and self.previous_block.original_line.is_triple_quoted_string + and not (current_line.is_class or current_line.is_def) ): before = 1 diff --git a/tests/data/cases/module_docstring_followed_by_class.py b/tests/data/cases/module_docstring_followed_by_class.py new file mode 100644 index 00000000000..6fdbfc8c240 --- /dev/null +++ b/tests/data/cases/module_docstring_followed_by_class.py @@ -0,0 +1,11 @@ +# flags: --preview +"""Two blank lines between module docstring and a class.""" +class MyClass: + pass + +# output +"""Two blank lines between module docstring and a class.""" + + +class MyClass: + pass diff --git a/tests/data/cases/module_docstring_followed_by_function.py b/tests/data/cases/module_docstring_followed_by_function.py new file mode 100644 index 00000000000..5913a59e1fe --- /dev/null +++ b/tests/data/cases/module_docstring_followed_by_function.py @@ -0,0 +1,11 @@ +# flags: --preview +"""Two blank lines between module docstring and a function def.""" +def function(): + pass + +# output +"""Two blank lines between module docstring and a function def.""" + + +def function(): + pass