From 69d0caabe7d6e154a7846b043c55bb5c3c028b78 Mon Sep 17 00:00:00 2001 From: Ely Ronnen Date: Sun, 10 Sep 2023 19:28:44 +0300 Subject: [PATCH] fix D417 error with function docstrings with dashed lines (#7251) ## Summary Fix https://github.com/astral-sh/ruff/issues/7250, False positive D417 for docstrings with dashed lines. ## Test Plan Tested on the example in https://github.com/astral-sh/ruff/issues/7250 --- .../ruff/resources/test/fixtures/pydocstyle/D417.py | 13 +++++++++++++ crates/ruff/src/rules/pydocstyle/rules/sections.rs | 13 +++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pydocstyle/D417.py b/crates/ruff/resources/test/fixtures/pydocstyle/D417.py index 30f4f2c6a8209..8c84a7eecd1dd 100644 --- a/crates/ruff/resources/test/fixtures/pydocstyle/D417.py +++ b/crates/ruff/resources/test/fixtures/pydocstyle/D417.py @@ -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: diff --git a/crates/ruff/src/rules/pydocstyle/rules/sections.rs b/crates/ruff/src/rules/pydocstyle/rules/sections.rs index 5303b77a77f2e..3d10a0c27aa69 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/sections.rs @@ -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 { @@ -1798,7 +1796,9 @@ fn args_section(context: &SectionContext) -> FxHashSet { 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); @@ -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 == '-') +}