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

[pydocstyle] Avoid non-character breaks in over-indentation (D208) #8866

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D208.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Platform:
""" Remove sampler
Args:
    Returns:
"""
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pydocstyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod tests {
#[test_case(Rule::NoBlankLineBeforeFunction, Path::new("D.py"))]
#[test_case(Rule::BlankLinesBetweenHeaderAndContent, Path::new("sections.py"))]
#[test_case(Rule::OverIndentation, Path::new("D.py"))]
#[test_case(Rule::OverIndentation, Path::new("D208.py"))]
#[test_case(Rule::NoSignature, Path::new("D.py"))]
#[test_case(Rule::SurroundingWhitespace, Path::new("D.py"))]
#[test_case(Rule::DocstringStartsWithThis, Path::new("D.py"))]
Expand Down
20 changes: 14 additions & 6 deletions crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
let mut has_seen_tab = docstring.indentation.contains('\t');
let mut is_over_indented = true;
let mut over_indented_lines = vec![];
let mut over_indented_offset = TextSize::from(u32::MAX);
let mut over_indented_offset = usize::MAX;

for i in 0..lines.len() {
// First lines and continuations doesn't need any indentation.
Expand Down Expand Up @@ -220,9 +220,9 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
if line_indent.len() > docstring.indentation.len() {
over_indented_lines.push(line);

// Track the _smallest_ offset we see
// Track the _smallest_ offset we see, in terms of characters.
over_indented_offset = std::cmp::min(
line_indent.text_len() - docstring.indentation.text_len(),
line_indent.chars().count() - docstring.indentation.chars().count(),
over_indented_offset,
);
} else {
Expand All @@ -247,15 +247,23 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
let indent = clean_space(docstring.indentation);

// We report over-indentation on every line. This isn't great, but
// enables fix.
// enables the fix capability.
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(line.start()));
let edit = if indent.is_empty() {
Edit::range_deletion(TextRange::at(line.start(), line_indent.text_len()))
Edit::deletion(line.start(), line_indent.text_len())
} else {
// Convert the character count to an offset within the source.
let offset = checker
.locator()
.after(line.start() + indent.text_len())
.chars()
.take(over_indented_offset)
.map(TextLen::text_len)
.sum::<TextSize>();
Edit::range_replacement(
indent.clone(),
TextRange::at(line.start(), indent.text_len() + over_indented_offset),
TextRange::at(line.start(), indent.text_len() + offset),
)
};
diagnostic.set_fix(Fix::safe_edit(edit));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
---
D208.py:3:1: D208 [*] Docstring is over-indented
|
1 | class Platform:
2 | """ Remove sampler
3 | Args:
| D208
4 |     Returns:
5 | """
|
= help: Remove over-indentation

Safe fix
1 1 | class Platform:
2 2 | """ Remove sampler
3 |- Args:
3 |+ Args:
4 4 |     Returns:
5 5 | """

D208.py:4:1: D208 [*] Docstring is over-indented
|
2 | """ Remove sampler
3 | Args:
4 |     Returns:
| D208
5 | """
|
= help: Remove over-indentation

Safe fix
1 1 | class Platform:
2 2 | """ Remove sampler
3 3 | Args:
4 |-     Returns:
4 |+ Returns:
5 5 | """

D208.py:5:1: D208 [*] Docstring is over-indented
|
3 | Args:
4 |     Returns:
5 | """
| D208
|
= help: Remove over-indentation

Safe fix
2 2 | """ Remove sampler
3 3 | Args:
4 4 |     Returns:
5 |- """
5 |+ """


Loading