From 58a2d600da5188161ab3e7b030bdbb5ed9aeb058 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 30 Oct 2022 13:02:55 -0400 Subject: [PATCH] Avoid flagging D202 for inner functions and classes (#518) --- src/pydocstyle/plugins.rs | 21 +++++------ .../ruff__linter__tests__D202_D.py.snap | 36 ------------------- 2 files changed, 8 insertions(+), 49 deletions(-) diff --git a/src/pydocstyle/plugins.rs b/src/pydocstyle/plugins.rs index 57f5d33b8f..3263685880 100644 --- a/src/pydocstyle/plugins.rs +++ b/src/pydocstyle/plugins.rs @@ -208,15 +208,13 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio .skip(1) .take_while(|line| line.trim().is_empty()) .count(); - // Report a D202 violation if the docstring is followed by a blank line and the - // blank line is not itself followed by an inner function or class. - let expected_blank_lines_after = - if INNER_FUNCTION_OR_CLASS_REGEX.is_match(after) { - 1 - } else { - 0 - }; - if blank_lines_after != expected_blank_lines_after { + + // Avoid D202 violations for blank lines followed by inner functions or classes. + if blank_lines_after == 1 && INNER_FUNCTION_OR_CLASS_REGEX.is_match(after) { + return; + } + + if blank_lines_after != 0 { let mut check = Check::new( CheckKind::NoBlankLineAfterFunction(blank_lines_after), Range::from_located(docstring), @@ -224,10 +222,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio if checker.patch() { // Delete the blank line after the docstring. check.amend(Fix::deletion( - Location::new( - docstring.location.row() + 1 + expected_blank_lines_after, - 1, - ), + Location::new(docstring.location.row() + 1, 1), Location::new(docstring.location.row() + 1 + blank_lines_after, 1), )); } diff --git a/src/snapshots/ruff__linter__tests__D202_D.py.snap b/src/snapshots/ruff__linter__tests__D202_D.py.snap index 383b01a771..8a02d844f5 100644 --- a/src/snapshots/ruff__linter__tests__D202_D.py.snap +++ b/src/snapshots/ruff__linter__tests__D202_D.py.snap @@ -2,24 +2,6 @@ source: src/linter.rs expression: checks --- -- kind: - NoBlankLineAfterFunction: 0 - location: - row: 79 - column: 5 - end_location: - row: 79 - column: 33 - fix: - patch: - content: "" - location: - row: 81 - column: 1 - end_location: - row: 80 - column: 1 - applied: false - kind: NoBlankLineAfterFunction: 1 location: @@ -56,22 +38,4 @@ expression: checks row: 148 column: 1 applied: false -- kind: - NoBlankLineAfterFunction: 0 - location: - row: 453 - column: 5 - end_location: - row: 453 - column: 24 - fix: - patch: - content: "" - location: - row: 455 - column: 1 - end_location: - row: 454 - column: 1 - applied: false