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

Fix an issue where extra empty lines are added. #3470

Merged
merged 2 commits into from Dec 21, 2022
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
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -23,6 +23,8 @@
regular and f-strings start with an empty span (#3463)
- Fix a crash in preview advanced string processing where a standalone comment is placed
before a dict's value (#3469)
- Fix an issue where extra empty lines are added when a decorator has `# fmt: skip`
applied or there is a standalone comment between decorators (#3470)
- Do not put the closing quotes in a docstring on a separate line, even if the line is
too long (#3430)
- Long values in dict literals are now wrapped in parentheses; correspondingly
Expand Down
3 changes: 2 additions & 1 deletion src/black/lines.py
Expand Up @@ -520,7 +520,8 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
and (self.semantic_leading_comment is None or before)
):
self.semantic_leading_comment = block
elif not current_line.is_decorator:
# `or before` means this decorator already has an empty line before
elif not current_line.is_decorator or before:
self.semantic_leading_comment = None

self.previous_line = current_line
Expand Down
51 changes: 51 additions & 0 deletions tests/data/preview/comments9.py
Expand Up @@ -114,6 +114,31 @@ def first_method(self):
pass


# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function


@decorator1
@decorator2 # fmt: skip
def bar():
pass


# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function.
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.

@decorator1
# A standalone comment
def bar():
pass


# output


Expand Down Expand Up @@ -252,3 +277,29 @@ class MyClass:
# More comments.
def first_method(self):
pass


# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function


@decorator1
@decorator2 # fmt: skip
def bar():
pass


# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function.
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.


@decorator1
# A standalone comment
def bar():
pass