Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<!-- Changes that affect Black's preview style -->

- Remove parentheses around sole list items (#4312)
- Collapse multiple empty lines after an import into one (#4489)

### Configuration

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 @@ -41,6 +41,8 @@ Currently, the following features are included in the preview style:
- `remove_lone_list_item_parens`: remove redundant parentheses around lone list items
(depends on unstable `hug_parens_with_braces_and_square_brackets` feature in some
cases)
- `always_one_newline_after_import`: Always force one blank line after import
statements, except when the line after the import is a comment or an import statement

(labels/unstable-features)=

Expand Down
9 changes: 9 additions & 0 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: # noqa: C9
current_line, before, user_had_newline
)

if (
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
):
return 1, 0

if (
self.previous_line.is_import
and not current_line.is_import
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class Preview(Enum):
# hug_parens_with_braces_and_square_brackets to remove parens in some cases
remove_lone_list_item_parens = 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 @@ -92,7 +92,8 @@
"remove_redundant_guard_parens",
"parens_for_long_if_clauses_in_case_block",
"remove_lone_list_item_parens",
"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
180 changes: 180 additions & 0 deletions tests/data/cases/preview_import_line_collapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# flags: --preview
Comment thread
kastkeepitjumpinlikekangaroos marked this conversation as 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__)


from middleman.authentication import validate_oauth_token



logger = logging.getLogger(__name__)


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



try:
import os
except Exception:
pass

try:
import os
def func():
a = 1
except Exception:
pass


# case 5 multiple imports
import os
import os

import os
import os





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


# case 6 import in function
def func():
print()
import os
def func():
pass
print()


def func():
import os
a = 1
print()


def func():
import os


a = 1
print()


def func():
import os



a = 1
print()

# 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__)


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)


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

try:
import os
except Exception:
pass

try:
import os

def func():
a = 1

except Exception:
pass


# case 5 multiple imports
import os
import os

import os
import os

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


# case 6 import in function
def func():
print()
import os

def func():
pass

print()


def func():
import os

a = 1
print()


def func():
import os

a = 1
print()


def func():
import os

a = 1
print()