Skip to content

Commit d7508af

Browse files
Truncate to one empty line in stub files (#7558)
## Summary This PR modifies a variety of sites in which we insert up to two empty lines to instead truncate to at most one empty line in stub files. We already enforce this in _some_ places, but not all. ## Test Plan `cargo test` No changes in similarity (as expected, since this only impacts unformatted `.pyi` files). Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 323 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 323 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 |
1 parent c3774e1 commit d7508af

File tree

4 files changed

+506
-6
lines changed

4 files changed

+506
-6
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
###
2+
# Blank lines around functions
3+
###
4+
5+
x = 1
6+
7+
# comment
8+
9+
def f():
10+
pass
11+
12+
13+
if True:
14+
x = 1
15+
16+
# comment
17+
18+
def f():
19+
pass
20+
21+
22+
x = 1
23+
24+
25+
26+
# comment
27+
28+
def f():
29+
pass
30+
31+
32+
x = 1
33+
34+
35+
36+
# comment
37+
def f():
38+
pass
39+
40+
41+
x = 1
42+
43+
# comment
44+
45+
# comment
46+
def f():
47+
pass
48+
49+
x = 1
50+
51+
# comment
52+
# comment
53+
54+
def f():
55+
pass
56+
57+
x = 1
58+
59+
# comment
60+
# comment
61+
def f():
62+
pass
63+
64+
65+
x = 1
66+
67+
68+
# comment
69+
70+
71+
72+
# comment
73+
74+
75+
76+
def f():
77+
pass
78+
# comment
79+
80+
81+
def f():
82+
pass
83+
84+
# comment
85+
86+
def f():
87+
pass
88+
89+
90+
# comment
91+
92+
###
93+
# Blank lines around imports.
94+
###
95+
96+
def f():
97+
import x
98+
# comment
99+
import y
100+
101+
102+
def f():
103+
import x
104+
105+
# comment
106+
import y
107+
108+
109+
def f():
110+
import x
111+
# comment
112+
113+
import y
114+
115+
116+
def f():
117+
import x
118+
# comment
119+
120+
121+
import y
122+
123+
124+
def f():
125+
import x
126+
127+
128+
# comment
129+
import y
130+
131+
132+
def f():
133+
import x
134+
135+
# comment
136+
137+
import y
138+
139+
140+
def f():
141+
import x # comment
142+
# comment
143+
144+
import y
145+
146+
147+
def f(): pass # comment
148+
# comment
149+
150+
x = 1
151+
152+
153+
def f():
154+
pass
155+
156+
157+
158+
159+
# comment
160+
161+
x = 1

crates/ruff_python_formatter/src/comments/format.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,14 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLines {
308308
NodeLevel::TopLevel => match self.lines {
309309
0 | 1 => write!(f, [hard_line_break()]),
310310
2 => write!(f, [empty_line()]),
311-
_ => write!(f, [empty_line(), empty_line()]),
311+
_ => match f.options().source_type() {
312+
PySourceType::Stub => {
313+
write!(f, [empty_line()])
314+
}
315+
PySourceType::Python | PySourceType::Ipynb => {
316+
write!(f, [empty_line(), empty_line()])
317+
}
318+
},
312319
},
313320

314321
NodeLevel::CompoundStatement => match self.lines {

crates/ruff_python_formatter/src/statement/suite.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
22
use ruff_python_ast::helpers::is_compound_statement;
33
use ruff_python_ast::node::AnyNodeRef;
4-
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, Stmt, Suite};
4+
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, PySourceType, Stmt, Suite};
55
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
66
use ruff_text_size::{Ranged, TextRange};
77

@@ -192,7 +192,14 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
192192
SuiteKind::TopLevel => {
193193
match lines_after_ignoring_trivia(preceding.end(), source) {
194194
0..=2 => empty_line().fmt(f)?,
195-
_ => write!(f, [empty_line(), empty_line()])?,
195+
_ => match source_type {
196+
PySourceType::Stub => {
197+
empty_line().fmt(f)?;
198+
}
199+
PySourceType::Python | PySourceType::Ipynb => {
200+
write!(f, [empty_line(), empty_line()])?;
201+
}
202+
},
196203
}
197204
}
198205
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
@@ -225,8 +232,15 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
225232
match lines_before(start, source) {
226233
0 | 1 => hard_line_break().fmt(f)?,
227234
2 => empty_line().fmt(f)?,
228-
3.. => match self.kind {
229-
SuiteKind::TopLevel => write!(f, [empty_line(), empty_line()])?,
235+
_ => match self.kind {
236+
SuiteKind::TopLevel => match source_type {
237+
PySourceType::Stub => {
238+
empty_line().fmt(f)?;
239+
}
240+
PySourceType::Python | PySourceType::Ipynb => {
241+
write!(f, [empty_line(), empty_line()])?;
242+
}
243+
},
230244
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
231245
empty_line().fmt(f)?;
232246
}
@@ -280,7 +294,14 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
280294
NodeLevel::TopLevel => match count_lines(end) {
281295
0 | 1 => hard_line_break().fmt(f)?,
282296
2 => empty_line().fmt(f)?,
283-
_ => write!(f, [empty_line(), empty_line()])?,
297+
_ => match source_type {
298+
PySourceType::Stub => {
299+
empty_line().fmt(f)?;
300+
}
301+
PySourceType::Python | PySourceType::Ipynb => {
302+
write!(f, [empty_line(), empty_line()])?;
303+
}
304+
},
284305
},
285306
NodeLevel::CompoundStatement => match count_lines(end) {
286307
0 | 1 => hard_line_break().fmt(f)?,

0 commit comments

Comments
 (0)