Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove blank lines before class docstring #3692

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/black/lines.py
Expand Up @@ -634,6 +634,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
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:
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions 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...
"""