Skip to content

Commit a3e67ab

Browse files
Add newlines before comments in E305 (#12606)
## Summary There's still a problem here. Given: ```python class Class(): pass # comment # another comment a = 1 ``` We only add one newline before `a = 1` on the first pass, because `max_precedling_blank_lines` is 1... We then add the second newline on the second pass, so it ends up in the right state, but the logic is clearly wonky. Closes #11508.
1 parent ee0518e commit a3e67ab

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py

+12
Original file line numberDiff line numberDiff line change
@@ -962,3 +962,15 @@ def test_update():
962962
def test_clientmodel():
963963
pass
964964
# end
965+
966+
967+
# E305
968+
969+
class A:
970+
pass
971+
972+
# ====== Cool constants ========
973+
BANANA = 100
974+
APPLE = 200
975+
976+
# end

crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,6 @@ impl<'a> BlankLinesChecker<'a> {
902902
line.first_token_range,
903903
);
904904

905-
// Check if the preceding comment
906-
907905
if let Some(blank_lines_range) = line.blank_lines.range() {
908906
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
909907
self.stylist
@@ -1029,10 +1027,10 @@ impl<'a> BlankLinesChecker<'a> {
10291027
)));
10301028
} else {
10311029
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
1032-
self.stylist
1033-
.line_ending()
1034-
.repeat(BLANK_LINES_TOP_LEVEL as usize),
1035-
self.locator.line_start(line.first_token_range.start()),
1030+
self.stylist.line_ending().repeat(
1031+
(BLANK_LINES_TOP_LEVEL - line.preceding_blank_lines.count()) as usize,
1032+
),
1033+
self.locator.line_start(state.last_non_comment_line_end),
10361034
)));
10371035
}
10381036

crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E305_E30.py.snap

+29-14
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ E30.py:798:1: E305 [*] Expected 2 blank lines after class or function definition
1515
796 796 |
1616
797 797 | # another comment
1717
798 |+
18-
799 |+
19-
798 800 | fn()
20-
799 801 | # end
21-
800 802 |
18+
798 799 | fn()
19+
799 800 | # end
20+
800 801 |
2221

2322
E30.py:809:1: E305 [*] Expected 2 blank lines after class or function definition, found (1)
2423
|
@@ -34,10 +33,9 @@ E30.py:809:1: E305 [*] Expected 2 blank lines after class or function definition
3433
807 807 |
3534
808 808 | # another comment
3635
809 |+
37-
810 |+
38-
809 811 | a = 1
39-
810 812 | # end
40-
811 813 |
36+
809 810 | a = 1
37+
810 811 | # end
38+
811 812 |
4139

4240
E30.py:821:1: E305 [*] Expected 2 blank lines after class or function definition, found (1)
4341
|
@@ -70,14 +68,13 @@ E30.py:833:1: E305 [*] Expected 2 blank lines after class or function definition
7068
= help: Add missing blank line(s)
7169

7270
Safe fix
71+
829 829 | def a():
7372
830 830 | print()
7473
831 831 |
75-
832 832 | # Two spaces before comments, too.
76-
833 |+
77-
834 |+
78-
833 835 | if a():
79-
834 836 | a()
80-
835 837 | # end
74+
832 |+
75+
832 833 | # Two spaces before comments, too.
76+
833 834 | if a():
77+
834 835 | a()
8178

8279
E30.py:846:1: E305 [*] Expected 2 blank lines after class or function definition, found (1)
8380
|
@@ -98,3 +95,21 @@ E30.py:846:1: E305 [*] Expected 2 blank lines after class or function definition
9895
846 847 | if __name__ == '__main__':
9996
847 848 | main()
10097
848 849 | # end
98+
99+
E30.py:973:1: E305 [*] Expected 2 blank lines after class or function definition, found (1)
100+
|
101+
972 | # ====== Cool constants ========
102+
973 | BANANA = 100
103+
| ^^^^^^ E305
104+
974 | APPLE = 200
105+
|
106+
= help: Add missing blank line(s)
107+
108+
Safe fix
109+
969 969 | class A:
110+
970 970 | pass
111+
971 971 |
112+
972 |+
113+
972 973 | # ====== Cool constants ========
114+
973 974 | BANANA = 100
115+
974 975 | APPLE = 200

crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__blank_lines_E305_notebook.snap

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ E30.ipynb:55:1: E305 [*] Expected 2 blank lines after class or function definiti
1616
53 53 |
1717
54 54 | # another comment
1818
55 |+
19-
56 |+
20-
55 57 | fn()
21-
56 58 | # end
22-
57 59 | # E306:3:5
19+
55 56 | fn()
20+
56 57 | # end
21+
57 58 | # E306:3:5

0 commit comments

Comments
 (0)