From 8e9ddee392326832016d3a10285b55bf9e2a8319 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 8 May 2024 11:19:27 -0400 Subject: [PATCH] Ignore end-of-line comments when determining blank line rules (#11342) ## Summary Closes https://github.com/astral-sh/ruff/issues/11331. --- .../test/fixtures/pycodestyle/E30.py | 22 ++++++++++++++----- .../rules/pycodestyle/rules/blank_lines.rs | 6 +++-- ...ules__pycodestyle__tests__E303_E30.py.snap | 6 ++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py index c6f10680a8985..3e8a6d093b559 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py @@ -439,7 +439,7 @@ def b(): # no error def test(): pass - + # Wrongly indented comment pass # end @@ -615,13 +615,13 @@ def g(): # E302 class Test: - + pass - + def method1(): return 1 - - + + def method2(): return 22 # end @@ -762,7 +762,7 @@ def b(self): def fn(): pass - + pass # end @@ -933,3 +933,13 @@ def a(): async def b(): pass # end + + +# no error +@overload +def arrow_strip_whitespace(obj: Table, /, *cols: str) -> Table: ... +@overload +def arrow_strip_whitespace(obj: Array, /, *cols: str) -> Array: ... # type: ignore[misc] +def arrow_strip_whitespace(obj, /, *cols): + ... +# end diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs index d9cf595ffc9d4..9aa19c5afddf7 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs @@ -352,7 +352,7 @@ struct LogicalLineInfo { kind: LogicalLineKind, first_token_range: TextRange, - // The token's kind right before the newline ending the logical line. + // The kind of the last non-trivia token before the newline ending the logical line. last_token: TokenKind, // The end of the logical line including the newline. @@ -549,7 +549,9 @@ impl<'a> Iterator for LinePreprocessor<'a> { _ => {} } - last_token = token_kind; + if !token_kind.is_trivia() { + last_token = token_kind; + } } None diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap index f9956a441928d..e9c1ee6919a49 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap @@ -13,8 +13,8 @@ E30.py:625:2: E303 [*] Too many blank lines (2) ℹ Safe fix 621 621 | def method1(): 622 622 | return 1 -623 623 | -624 |- +623 623 | +624 |- 625 624 | def method2(): 626 625 | return 22 627 626 | # end @@ -242,7 +242,7 @@ E30.py:766:5: E303 [*] Too many blank lines (2) 762 762 | def fn(): 763 763 | pass 764 764 | -765 |- +765 |- 766 765 | pass 767 766 | # end 768 767 |