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

Don't raise D208 when last line is non-empty #13372

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
44 changes: 44 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D208.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,47 @@ def memory_test():
"""
   参数含义:precision:精确到小数点后几位
"""


def right_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass


def over_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass


def under_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass



def right_indent_quotes_same_line_with_multiple_empty_lines(a: int) -> None:
"""Foo.
Parameters
----------
a : int




A parameter."""
pass
51 changes: 50 additions & 1 deletion crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_python_ast::docstrings::{clean_space, leading_space};
use ruff_source_file::NewlineWithTrailingNewline;
use ruff_text_size::{Ranged, TextSize};
use ruff_text_size::{TextLen, TextRange};
use std::cmp::Ordering;

use crate::checkers::ast::Checker;
use crate::docstrings::Docstring;
Expand Down Expand Up @@ -291,10 +292,58 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
let line_indent = leading_space(last);
let line_indent_size = line_indent.chars().count();
if line_indent_size > docstring_indent_size {
let offset_len = if line_indent.text_len() == last.text_len() {
line_indent_size
// The trailing quotes are not on a separate line.
} else {
let over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
match over_indented_size.cmp(&docstring_indent_size) {
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
// If indentation is exactly the same, no changes are needed.
// Example:
// ```
// def f():
// """A docstring.
// Args:
// ^^^^This line is correctly indented (4 spaces)."""
// ```
Ordering::Equal => {
return;
}
ukyen8 marked this conversation as resolved.
Show resolved Hide resolved
// If the indentation is less then docstring_indent_size,
// return over_indented_size, which will be corrected later to match `docstring.indentation`.
// Example:
// ```
// def f():
// """A docstring.
// ^^This line is slightly over-indented (2 spaces)."""
// ```
Ordering::Less => over_indented_size,
// If the indentation is significantly larger than needed,
// return the excess spaces to be replaced with the correct `docstring.indentation` later.
// Example:
// ```
// def f():
// """A docstring.
// ^^^^^^^^This line is over-indented by 8 spaces; it needs fixing."""
// ```
Ordering::Greater => {
line_indent_size - (over_indented_size - docstring_indent_size)
}
}
};

let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
let indent = clean_space(docstring.indentation);
let range = TextRange::at(last.start(), line_indent.text_len());
let offset = checker
.locator()
.after(last.start())
.chars()
.take(offset_len)
.map(TextLen::text_len)
.sum::<TextSize>();
let range = TextRange::at(last.start(), offset);
let edit = if indent.is_empty() {
Edit::range_deletion(range)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,42 @@ D208.py:10:1: D208 [*] Docstring is over-indented
12 12 |
13 13 | def memory_test():

D208.py:35:1: D208 [*] Docstring is over-indented
|
33 | ----------
34 | a : int
35 | A parameter."""
| D208
36 | pass
|
= help: Remove over-indentation

ℹ Safe fix
32 32 | Parameters
33 33 | ----------
34 34 | a : int
35 |- A parameter."""
35 |+ A parameter."""
36 36 | pass
37 37 |
38 38 |

D208.py:45:1: D208 [*] Docstring is over-indented
|
43 | ----------
44 | a : int
45 | A parameter."""
| D208
46 | pass
|
= help: Remove over-indentation

ℹ Safe fix
42 42 | Parameters
43 43 | ----------
44 44 | a : int
45 |- A parameter."""
45 |+ A parameter."""
46 46 | pass
47 47 |
48 48 |
Loading