Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e54f86b

Browse files
Two blank lines after an import should be reduced to one (psf#4489)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 96ca1b6 commit e54f86b

7 files changed

Lines changed: 195 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<!-- Changes that affect Black's preview style -->
1919

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

2223
### Configuration
2324

docs/the_black_code_style/future_style.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Currently, the following features are included in the preview style:
4141
- `remove_lone_list_item_parens`: remove redundant parentheses around lone list items
4242
(depends on unstable `hug_parens_with_braces_and_square_brackets` feature in some
4343
cases)
44+
- `always_one_newline_after_import`: Always force one blank line after import
45+
statements, except when the line after the import is a comment or an import statement
4446

4547
(labels/unstable-features)=
4648

src/black/lines.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: # noqa: C9
671671
current_line, before, user_had_newline
672672
)
673673

674+
if (
675+
self.previous_line.is_import
676+
and self.previous_line.depth == 0
677+
and current_line.depth == 0
678+
and not current_line.is_import
679+
and Preview.always_one_newline_after_import in self.mode
680+
):
681+
return 1, 0
682+
674683
if (
675684
self.previous_line.is_import
676685
and not current_line.is_import

src/black/mode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class Preview(Enum):
214214
# hug_parens_with_braces_and_square_brackets to remove parens in some cases
215215
remove_lone_list_item_parens = auto()
216216
pep646_typed_star_arg_type_var_tuple = auto()
217+
always_one_newline_after_import = auto()
217218

218219

219220
UNSTABLE_FEATURES: set[Preview] = {

src/black/resources/black.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
"remove_redundant_guard_parens",
9393
"parens_for_long_if_clauses_in_case_block",
9494
"remove_lone_list_item_parens",
95-
"pep646_typed_star_arg_type_var_tuple"
95+
"pep646_typed_star_arg_type_var_tuple",
96+
"always_one_newline_after_import"
9697
]
9798
},
9899
"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."

tests/data/cases/preview_comments7.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ def test_fails_invalid_post_data(
177177
MyLovelyCompanyTeamProjectComponent as component, # DRY
178178
)
179179

180-
181180
result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
182181

183182
result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# flags: --preview
2+
from middleman.authentication import validate_oauth_token
3+
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
# case 2 comment after import
9+
from middleman.authentication import validate_oauth_token
10+
#comment
11+
12+
logger = logging.getLogger(__name__)
13+
14+
15+
# case 3 comment after import
16+
from middleman.authentication import validate_oauth_token
17+
# comment
18+
logger = logging.getLogger(__name__)
19+
20+
21+
from middleman.authentication import validate_oauth_token
22+
23+
24+
25+
logger = logging.getLogger(__name__)
26+
27+
28+
# case 4 try catch with import after import
29+
import os
30+
import os
31+
32+
33+
34+
try:
35+
import os
36+
except Exception:
37+
pass
38+
39+
try:
40+
import os
41+
def func():
42+
a = 1
43+
except Exception:
44+
pass
45+
46+
47+
# case 5 multiple imports
48+
import os
49+
import os
50+
51+
import os
52+
import os
53+
54+
55+
56+
57+
58+
for i in range(10):
59+
print(i)
60+
61+
62+
# case 6 import in function
63+
def func():
64+
print()
65+
import os
66+
def func():
67+
pass
68+
print()
69+
70+
71+
def func():
72+
import os
73+
a = 1
74+
print()
75+
76+
77+
def func():
78+
import os
79+
80+
81+
a = 1
82+
print()
83+
84+
85+
def func():
86+
import os
87+
88+
89+
90+
a = 1
91+
print()
92+
93+
# output
94+
95+
96+
from middleman.authentication import validate_oauth_token
97+
98+
logger = logging.getLogger(__name__)
99+
100+
101+
# case 2 comment after import
102+
from middleman.authentication import validate_oauth_token
103+
104+
# comment
105+
106+
logger = logging.getLogger(__name__)
107+
108+
109+
# case 3 comment after import
110+
from middleman.authentication import validate_oauth_token
111+
112+
# comment
113+
logger = logging.getLogger(__name__)
114+
115+
116+
from middleman.authentication import validate_oauth_token
117+
118+
logger = logging.getLogger(__name__)
119+
120+
121+
# case 4 try catch with import after import
122+
import os
123+
import os
124+
125+
try:
126+
import os
127+
except Exception:
128+
pass
129+
130+
try:
131+
import os
132+
133+
def func():
134+
a = 1
135+
136+
except Exception:
137+
pass
138+
139+
140+
# case 5 multiple imports
141+
import os
142+
import os
143+
144+
import os
145+
import os
146+
147+
for i in range(10):
148+
print(i)
149+
150+
151+
# case 6 import in function
152+
def func():
153+
print()
154+
import os
155+
156+
def func():
157+
pass
158+
159+
print()
160+
161+
162+
def func():
163+
import os
164+
165+
a = 1
166+
print()
167+
168+
169+
def func():
170+
import os
171+
172+
a = 1
173+
print()
174+
175+
176+
def func():
177+
import os
178+
179+
a = 1
180+
print()

0 commit comments

Comments
 (0)