Skip to content

Commit c73c497

Browse files
[pydocstyle] Trim whitespace when removing blank lines after section (D413) (#10162)
Co-authored-by: Micha Reiser <[email protected]>
1 parent c9c98c4 commit c73c497

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

crates/ruff_linter/resources/test/fixtures/pydocstyle/D413.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ def func():
5757
5858
Returns:
5959
the value"""
60+
61+
62+
def func():
63+
"""Do something.
64+
65+
Args:
66+
x: the value
67+
with a hanging indent
68+
69+
Returns:
70+
the value
71+
"""

crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,21 +1665,30 @@ fn common_section(
16651665
.take_while(|line| line.trim().is_empty())
16661666
.count();
16671667
if num_blank_lines < 2 {
1668-
let mut diagnostic = Diagnostic::new(
1669-
BlankLineAfterLastSection {
1670-
name: context.section_name().to_string(),
1671-
},
1672-
docstring.range(),
1673-
);
1674-
// Add a newline after the section.
1675-
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
1668+
let del_len = if num_blank_lines == 1 {
1669+
// SAFETY: Guaranteed to not be None, because `num_blank_lines`is 1.
1670+
context.following_lines().next_back().unwrap().text_len()
1671+
} else {
1672+
TextSize::new(0)
1673+
};
1674+
1675+
let edit = Edit::replacement(
16761676
format!(
16771677
"{}{}",
16781678
line_end.repeat(2 - num_blank_lines),
16791679
docstring.indentation
16801680
),
1681+
context.end() - del_len,
16811682
context.end(),
1682-
)));
1683+
);
1684+
1685+
let mut diagnostic = Diagnostic::new(
1686+
BlankLineAfterLastSection {
1687+
name: context.section_name().to_string(),
1688+
},
1689+
docstring.range(),
1690+
);
1691+
diagnostic.set_fix(Fix::safe_edit(edit));
16831692
checker.diagnostics.push(diagnostic);
16841693
}
16851694
}

crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D413_D413.py.snap

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ D413.py:13:5: D413 [*] Missing blank line after last section ("Returns")
4646
18 18 |
4747
19 19 | Returns:
4848
20 20 | the value
49-
21 |+
49+
21 |+
5050
21 22 | """
5151
22 23 |
5252
23 24 |
@@ -75,5 +75,31 @@ D413.py:52:5: D413 [*] Missing blank line after last section ("Returns")
7575
59 |+ the value
7676
60 |+
7777
61 |+ """
78+
60 62 |
79+
61 63 |
80+
62 64 | def func():
7881

82+
D413.py:63:5: D413 [*] Missing blank line after last section ("Returns")
83+
|
84+
62 | def func():
85+
63 | """Do something.
86+
| _____^
87+
64 | |
88+
65 | | Args:
89+
66 | | x: the value
90+
67 | | with a hanging indent
91+
68 | |
92+
69 | | Returns:
93+
70 | | the value
94+
71 | | """
95+
| |___________^ D413
96+
|
97+
= help: Add blank line after "Returns"
7998

99+
Safe fix
100+
68 68 |
101+
69 69 | Returns:
102+
70 70 | the value
103+
71 |- """
104+
71 |+
105+
72 |+ """

crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D413_sections.py.snap

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ sections.py:120:5: D413 [*] Missing blank line after last section ("Returns")
4444
122 122 | Returns
4545
123 123 | -------
4646
124 124 | A value of some sort.
47-
125 |+
47+
125 |+
4848
125 126 | """
4949
126 127 |
5050
127 128 |
@@ -67,7 +67,7 @@ sections.py:170:5: D413 [*] Missing blank line after last section ("Returns")
6767
171 171 |
6868
172 172 | Returns
6969
173 173 | -------
70-
174 |+
70+
174 |+
7171
174 175 | """
7272
175 176 |
7373
176 177 |
@@ -89,7 +89,7 @@ sections.py:519:5: D413 [*] Missing blank line after last section ("Parameters")
8989
520 520 |
9090
521 521 | Parameters
9191
522 522 | ==========
92-
523 |+
92+
523 |+
9393
523 524 | """
9494
524 525 |
9595
525 526 |
@@ -111,7 +111,7 @@ sections.py:527:5: D413 [*] Missing blank line after last section ("Parameters")
111111
528 528 |
112112
529 529 | Parameters
113113
530 530 | ===========
114-
531 |+
114+
531 |+
115115
531 532 | """
116116
532 533 |
117117
533 534 |
@@ -135,7 +135,7 @@ sections.py:548:5: D413 [*] Missing blank line after last section ("Args")
135135
551 551 | Here's a note.
136136
552 552 |
137137
553 553 | returns:
138-
554 |+
138+
554 |+
139139
554 555 | """
140140
555 556 |
141141
556 557 |
@@ -159,7 +159,7 @@ sections.py:558:5: D413 [*] Missing blank line after last section ("Returns")
159159
561 561 | Here's a note.
160160
562 562 |
161161
563 563 | Returns:
162-
564 |+
162+
564 |+
163163
564 565 | """
164164
565 566 |
165165
566 567 |
@@ -185,9 +185,7 @@ sections.py:588:5: D413 [*] Missing blank line after last section ("Parameters")
185185
593 593 | A list of string parameters
186186
594 594 | value:
187187
595 595 | Some value
188-
596 |+
188+
596 |+
189189
596 597 | """
190190
597 598 |
191-
598 599 |
192-
193-
191+
598 599 |

0 commit comments

Comments
 (0)