Skip to content

Commit 7171281

Browse files
authored
Fix panic in pydocstyle D214 when docstring indentation is empty (#4216)
1 parent e9e194a commit 7171281

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""A module docstring with D214 violations
2+
3+
Returns
4+
-----
5+
valid returns
6+
7+
Args
8+
-----
9+
valid args
10+
"""
11+
12+
import os
13+
from .expected import Expectation
14+
15+
expectation = Expectation()
16+
expect = expectation.expect
17+
18+
expect(os.path.normcase(__file__ if __file__[-1] != 'c' else __file__[:-1]),
19+
"D214: Section is over-indented ('Returns')")
20+
expect(os.path.normcase(__file__ if __file__[-1] != 'c' else __file__[:-1]),
21+
"D214: Section is over-indented ('Args')")

crates/ruff/src/rules/pydocstyle/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ mod tests {
6868
#[test_case(Rule::UndocumentedPublicPackage, Path::new("D104/__init__.py"); "D104_1")]
6969
#[test_case(Rule::SectionNameEndsInColon, Path::new("D.py"); "D416")]
7070
#[test_case(Rule::SectionNotOverIndented, Path::new("sections.py"); "D214")]
71+
#[test_case(Rule::SectionNotOverIndented, Path::new("D214_module.py"); "D214_module")]
7172
#[test_case(Rule::SectionUnderlineAfterName, Path::new("sections.py"); "D408")]
7273
#[test_case(Rule::SectionUnderlineMatchesSectionLength, Path::new("sections.py"); "D409")]
7374
#[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"); "D215")]

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,14 @@ fn common_section(
619619
);
620620
if checker.patch(diagnostic.kind.rule()) {
621621
// Replace the existing indentation with whitespace of the appropriate length.
622-
diagnostic.set_fix(Edit::range_replacement(
623-
whitespace::clean(docstring.indentation),
624-
TextRange::at(context.range().start(), leading_space.text_len()),
625-
));
622+
let content = whitespace::clean(docstring.indentation);
623+
let fix_range = TextRange::at(context.range().start(), leading_space.text_len());
624+
625+
diagnostic.set_fix(if content.is_empty() {
626+
Edit::range_deletion(fix_range)
627+
} else {
628+
Edit::range_replacement(content, fix_range)
629+
});
626630
};
627631
checker.diagnostics.push(diagnostic);
628632
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
source: crates/ruff/src/rules/pydocstyle/mod.rs
3+
---
4+
D214_module.py:1:1: D214 [*] Section is over-indented ("Returns")
5+
|
6+
1 | / """A module docstring with D214 violations
7+
2 | |
8+
3 | | Returns
9+
4 | | -----
10+
5 | | valid returns
11+
6 | |
12+
7 | | Args
13+
8 | | -----
14+
9 | | valid args
15+
10 | | """
16+
| |___^ D214
17+
11 |
18+
12 | import os
19+
|
20+
= help: Remove over-indentation from "Returns"
21+
22+
Suggested fix
23+
1 1 | """A module docstring with D214 violations
24+
2 2 |
25+
3 |- Returns
26+
3 |+Returns
27+
4 4 | -----
28+
5 5 | valid returns
29+
6 6 |
30+
31+
D214_module.py:1:1: D214 [*] Section is over-indented ("Args")
32+
|
33+
1 | / """A module docstring with D214 violations
34+
2 | |
35+
3 | | Returns
36+
4 | | -----
37+
5 | | valid returns
38+
6 | |
39+
7 | | Args
40+
8 | | -----
41+
9 | | valid args
42+
10 | | """
43+
| |___^ D214
44+
11 |
45+
12 | import os
46+
|
47+
= help: Remove over-indentation from "Args"
48+
49+
Suggested fix
50+
4 4 | -----
51+
5 5 | valid returns
52+
6 6 |
53+
7 |- Args
54+
7 |+Args
55+
8 8 | -----
56+
9 9 | valid args
57+
10 10 | """
58+
59+

0 commit comments

Comments
 (0)