Skip to content

Commit

Permalink
Fix panic in pydocstyle D214 when docstring indentation is empty (#4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed May 4, 2023
1 parent e9e194a commit 7171281
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
21 changes: 21 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/D214_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""A module docstring with D214 violations
Returns
-----
valid returns
Args
-----
valid args
"""

import os
from .expected import Expectation

expectation = Expectation()
expect = expectation.expect

expect(os.path.normcase(__file__ if __file__[-1] != 'c' else __file__[:-1]),
"D214: Section is over-indented ('Returns')")
expect(os.path.normcase(__file__ if __file__[-1] != 'c' else __file__[:-1]),
"D214: Section is over-indented ('Args')")
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pydocstyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod tests {
#[test_case(Rule::UndocumentedPublicPackage, Path::new("D104/__init__.py"); "D104_1")]
#[test_case(Rule::SectionNameEndsInColon, Path::new("D.py"); "D416")]
#[test_case(Rule::SectionNotOverIndented, Path::new("sections.py"); "D214")]
#[test_case(Rule::SectionNotOverIndented, Path::new("D214_module.py"); "D214_module")]
#[test_case(Rule::SectionUnderlineAfterName, Path::new("sections.py"); "D408")]
#[test_case(Rule::SectionUnderlineMatchesSectionLength, Path::new("sections.py"); "D409")]
#[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"); "D215")]
Expand Down
12 changes: 8 additions & 4 deletions crates/ruff/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,14 @@ fn common_section(
);
if checker.patch(diagnostic.kind.rule()) {
// Replace the existing indentation with whitespace of the appropriate length.
diagnostic.set_fix(Edit::range_replacement(
whitespace::clean(docstring.indentation),
TextRange::at(context.range().start(), leading_space.text_len()),
));
let content = whitespace::clean(docstring.indentation);
let fix_range = TextRange::at(context.range().start(), leading_space.text_len());

diagnostic.set_fix(if content.is_empty() {
Edit::range_deletion(fix_range)
} else {
Edit::range_replacement(content, fix_range)
});
};
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
source: crates/ruff/src/rules/pydocstyle/mod.rs
---
D214_module.py:1:1: D214 [*] Section is over-indented ("Returns")
|
1 | / """A module docstring with D214 violations
2 | |
3 | | Returns
4 | | -----
5 | | valid returns
6 | |
7 | | Args
8 | | -----
9 | | valid args
10 | | """
| |___^ D214
11 |
12 | import os
|
= help: Remove over-indentation from "Returns"

Suggested fix
1 1 | """A module docstring with D214 violations
2 2 |
3 |- Returns
3 |+Returns
4 4 | -----
5 5 | valid returns
6 6 |

D214_module.py:1:1: D214 [*] Section is over-indented ("Args")
|
1 | / """A module docstring with D214 violations
2 | |
3 | | Returns
4 | | -----
5 | | valid returns
6 | |
7 | | Args
8 | | -----
9 | | valid args
10 | | """
| |___^ D214
11 |
12 | import os
|
= help: Remove over-indentation from "Args"

Suggested fix
4 4 | -----
5 5 | valid returns
6 6 |
7 |- Args
7 |+Args
8 8 | -----
9 9 | valid args
10 10 | """

0 comments on commit 7171281

Please sign in to comment.