From fe0f23528ed0937c8b8954017089a09af91cc460 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 27 Jun 2023 10:59:43 +0530 Subject: [PATCH 1/2] Replace same length equal line with dash line in D407 --- .../test/fixtures/pydocstyle/sections.py | 16 +++++++ .../src/rules/pydocstyle/rules/sections.rs | 30 ++++++++++--- ...__pydocstyle__tests__D407_sections.py.snap | 44 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pydocstyle/sections.py b/crates/ruff/resources/test/fixtures/pydocstyle/sections.py index 4bd805065c40b..fef7f2fba15c3 100644 --- a/crates/ruff/resources/test/fixtures/pydocstyle/sections.py +++ b/crates/ruff/resources/test/fixtures/pydocstyle/sections.py @@ -513,3 +513,19 @@ def implicit_string_concatenation(): A value of some sort. """"Extra content" + + +def replace_equals_with_dash(): + """Equal length equals should be replaced with dashes. + + Parameters + ========== + """ + + +def replace_equals_with_dash2(): + """Here, the length of equals is not the same. + + Parameters + =========== + """ diff --git a/crates/ruff/src/rules/pydocstyle/rules/sections.rs b/crates/ruff/src/rules/pydocstyle/rules/sections.rs index 2050c5f973cf3..1d2d64e8919f6 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/sections.rs @@ -13,7 +13,7 @@ use ruff_python_ast::docstrings::{clean_space, leading_space}; use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::is_staticmethod; use ruff_python_semantic::{Definition, Member, MemberKind}; -use ruff_python_whitespace::NewlineWithTrailingNewline; +use ruff_python_whitespace::{NewlineWithTrailingNewline, PythonWhitespace}; use ruff_textwrap::dedent; use crate::checkers::ast::Checker; @@ -492,6 +492,10 @@ fn blanks_and_section_underline( } } } else { + let equal_line_found = non_blank_line + .chars() + .all(|char| char.is_whitespace() || char == '='); + if checker.enabled(Rule::DashedUnderlineAfterSection) { let mut diagnostic = Diagnostic::new( DashedUnderlineAfterSection { @@ -507,11 +511,25 @@ fn blanks_and_section_underline( clean_space(docstring.indentation), "-".repeat(context.section_name().len()), ); - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::insertion( - content, - context.summary_range().end(), - ))); + if equal_line_found + && non_blank_line.trim_whitespace().len() == context.section_name().len() + { + // If an existing underline is an equal sign line of the appropriate length, + // replace it with a dashed line. + #[allow(deprecated)] + diagnostic.set_fix(Fix::unspecified(Edit::replacement( + content, + context.summary_range().end(), + non_blank_line.end(), + ))); + } else { + // Otherwise, insert a dashed line after the section header. + #[allow(deprecated)] + diagnostic.set_fix(Fix::unspecified(Edit::insertion( + content, + context.summary_range().end(), + ))); + } } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap index 33128a959e801..edddc3652e2d8 100644 --- a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap +++ b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap @@ -453,4 +453,48 @@ sections.py:499:9: D407 [*] Missing dashed underline after section ("Args") 505 506 | 506 507 | """ +sections.py:519:5: D407 [*] Missing dashed underline after section ("Parameters") + | +518 | def replace_equals_with_dash(): +519 | """Equal length equals should be replaced with dashes. + | _____^ +520 | | +521 | | Parameters +522 | | ========== +523 | | """ + | |_______^ D407 + | + = help: Add dashed line under "Parameters" + +ℹ Suggested fix +519 519 | """Equal length equals should be replaced with dashes. +520 520 | +521 521 | Parameters +522 |- ========== + 522 |+ ---------- +523 523 | """ +524 524 | +525 525 | + +sections.py:527:5: D407 [*] Missing dashed underline after section ("Parameters") + | +526 | def replace_equals_with_dash2(): +527 | """Here, the length of equals is not the same. + | _____^ +528 | | +529 | | Parameters +530 | | =========== +531 | | """ + | |_______^ D407 + | + = help: Add dashed line under "Parameters" + +ℹ Suggested fix +527 527 | """Here, the length of equals is not the same. +528 528 | +529 529 | Parameters + 530 |+ ---------- +530 531 | =========== +531 532 | """ + From a31b33b4a9c908ae5843eba5832b1fff6a51e1ef Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 27 Jun 2023 12:41:35 -0400 Subject: [PATCH 2/2] Fixture --- .../ruff__rules__pydocstyle__tests__D407_sections.py.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap index a214f232e8475..f0eac751331cb 100644 --- a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap +++ b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D407_sections.py.snap @@ -466,7 +466,7 @@ sections.py:519:5: D407 [*] Missing dashed underline after section ("Parameters" | = help: Add dashed line under "Parameters" -ℹ Suggested fix +ℹ Fix 519 519 | """Equal length equals should be replaced with dashes. 520 520 | 521 521 | Parameters @@ -489,7 +489,7 @@ sections.py:527:5: D407 [*] Missing dashed underline after section ("Parameters" | = help: Add dashed line under "Parameters" -ℹ Suggested fix +ℹ Fix 527 527 | """Here, the length of equals is not the same. 528 528 | 529 529 | Parameters