Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in pydocstyle D214 when docstring indentation is empty #4216

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]
zanieb marked this conversation as resolved.
Show resolved Hide resolved
#[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
14 changes: 10 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,16 @@ 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);
if content.is_empty() {
let start = context.range().start();
diagnostic.set_fix(Edit::deletion(start, start + leading_space.text_len()));
} else {
diagnostic.set_fix(Edit::range_replacement(
content,
TextRange::at(context.range().start(), leading_space.text_len()),
));
}
zanieb marked this conversation as resolved.
Show resolved Hide resolved
};
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 | """