Skip to content

Commit

Permalink
fix D417 error with function docstrings with dashed lines (#7251)
Browse files Browse the repository at this point in the history
## Summary

Fix #7250, False positive D417
for docstrings with dashed lines.

## Test Plan

Tested on the example in #7250
  • Loading branch information
eronnen authored Sep 10, 2023
1 parent 9cb5ce7 commit 69d0caa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 13 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/D417.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ def f(x, *, y, z):
"""
return x, y, z

def f(x):
"""Do something with valid description.
Args:
----
x: the value
Returns:
-------
the value
"""
return x


class Test:
def f(self, /, arg1: int) -> None:
Expand Down
13 changes: 9 additions & 4 deletions crates/ruff/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,9 +1377,7 @@ fn blanks_and_section_underline(
}

if let Some(non_blank_line) = following_lines.next() {
let dash_line_found = non_blank_line
.chars()
.all(|char| char.is_whitespace() || char == '-');
let dash_line_found = is_dashed_underline(&non_blank_line);

if dash_line_found {
if blank_lines_after_header > 0 {
Expand Down Expand Up @@ -1798,7 +1796,9 @@ fn args_section(context: &SectionContext) -> FxHashSet<String> {
let relevant_lines = std::iter::once(first_line)
.chain(following_lines)
.map(|l| l.as_str())
.filter(|line| line.starts_with(leading_space) || line.is_empty())
.filter(|line| {
line.is_empty() || (line.starts_with(leading_space) && !is_dashed_underline(line))
})
.join("\n");
let args_content = dedent(&relevant_lines);

Expand Down Expand Up @@ -1989,3 +1989,8 @@ fn parse_google_sections(
}
}
}

fn is_dashed_underline(line: &str) -> bool {
let trimmed_line = line.trim();
!trimmed_line.is_empty() && trimmed_line.chars().all(|char| char == '-')
}

0 comments on commit 69d0caa

Please sign in to comment.