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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

- Collapse multiple lines into 1 after import (#4489)

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
2 changes: 2 additions & 0 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Currently, the following features are included in the preview style:
blocks when the line is too long
- `pep646_typed_star_arg_type_var_tuple`: fix type annotation spacing between * and more
complex type variable tuple (i.e. `def fn(*args: *tuple[*Ts, T]) -> None: pass`)
- `always_one_newline_after_import`: Always force one newline after import statements
except for when the line after the import is a comment or an import statement

(labels/unstable-features)=

Expand Down
11 changes: 10 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,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 Preview.always_one_newline_after_import in self.mode
else min(before, max_allowed)
)
first_leaf.prefix = ""
else:
before = 0
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class Preview(Enum):
remove_redundant_guard_parens = auto()
parens_for_long_if_clauses_in_case_block = auto()
pep646_typed_star_arg_type_var_tuple = auto()
always_one_newline_after_import = auto()


UNSTABLE_FEATURES: set[Preview] = {
Expand Down
3 changes: 2 additions & 1 deletion src/black/resources/black.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"docstring_check_for_newline",
"remove_redundant_guard_parens",
"parens_for_long_if_clauses_in_case_block",
"pep646_typed_star_arg_type_var_tuple"
"pep646_typed_star_arg_type_var_tuple",
"always_one_newline_after_import"
]
},
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
Expand Down
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
41 changes: 41 additions & 0 deletions tests/data/cases/preview_import_line_collapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# flags: --preview
kastkeepitjumpinlikekangaroos marked this conversation as resolved.
Show resolved Hide resolved
from middleman.authentication import validate_oauth_token


logger = logging.getLogger(__name__)


# case 2 comment after import
from middleman.authentication import validate_oauth_token
#comment

logger = logging.getLogger(__name__)


# case 3 comment after import
from middleman.authentication import validate_oauth_token
# comment
logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)


# case 2 comment after import
from middleman.authentication import validate_oauth_token

# comment

logger = logging.getLogger(__name__)


# case 3 comment after import
from middleman.authentication import validate_oauth_token

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



logger = logging.getLogger(__name__)


# case 2 try catch with import after import
import os
import os



try:
import os
except Exception:
pass


# case 3 multiple imports
import os
import os

import os
import os





for i in range(10):
print(i)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)


# case 2 try catch with import after import
import os
import os

try:
import os
except Exception:
pass


# case 3 multiple imports
import os
import os

import os
import os

for i in range(10):
print(i)
Loading