Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 11 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,17 @@ 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 = (
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
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
13 changes: 13 additions & 0 deletions tests/data/cases/preview_import_line_collapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# flags: --preview
from middleman.authentication import validate_oauth_token


logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
14 changes: 14 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,14 @@
# flags: --preview
from middleman.authentication import validate_oauth_token



logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
Loading