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

Two blank lines after an import should be reduced to one #4489

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<!-- Changes that affect Black's stable style -->

- Collapse multiple lines into 1 after import (#4489)
- Fix formatting cells in IPython notebooks with magic methods and starting or trailing
empty lines (#4484)

Expand Down
11 changes: 10 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,16 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: # noqa: C9
# Consume the first leaf's extra newlines.
first_leaf = current_line.leaves[0]
before = first_leaf.prefix.count("\n")
before = min(before, max_allowed)
before = (
kastkeepitjumpinlikekangaroos marked this conversation as resolved.
Show resolved Hide resolved
1
if self.previous_line
and self.previous_line.is_import
and self.previous_line.depth == 0
and current_line.depth == 0
and not current_line.is_import
and not current_line.is_comment
else min(before, max_allowed)
)
first_leaf.prefix = ""
else:
before = 0
Expand Down
12 changes: 12 additions & 0 deletions tests/data/cases/import_line_collapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from middleman.authentication import validate_oauth_token


logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
13 changes: 13 additions & 0 deletions tests/data/cases/import_line_collapse_3_to_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from middleman.authentication import validate_oauth_token



logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
1 change: 0 additions & 1 deletion tests/data/cases/preview_comments7.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def test_fails_invalid_post_data(
MyLovelyCompanyTeamProjectComponent as component, # DRY
)


result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down