From 84557015ef6dae8489b5c47f21296e5562e0d083 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 16 May 2024 17:02:55 +0530 Subject: [PATCH] Consider soft keywords for `E27` rules --- .../test/fixtures/pycodestyle/E27.py | 13 +++++++ .../missing_whitespace_after_keyword.rs | 2 +- .../pycodestyle/rules/logical_lines/mod.rs | 2 +- .../whitespace_around_keywords.rs | 6 ++-- ...ules__pycodestyle__tests__E271_E27.py.snap | 18 ++++++++++ ...ules__pycodestyle__tests__E273_E27.py.snap | 18 ++++++++++ ...ules__pycodestyle__tests__E275_E27.py.snap | 35 +++++++++++++++++++ 7 files changed, 89 insertions(+), 5 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E27.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E27.py index 7fb6fbb4f021bb..6246a5f46097db 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E27.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E27.py @@ -63,3 +63,16 @@ def f(): #: Okay def f(): return 1 + +# Soft keywords + +#: E271 +type Number = int + +#: E273 +type Number = int + +#: E275 +match(foo): + case(1): + pass diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs index d8dc7762c748a7..296d9514bda6e9 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs @@ -52,7 +52,7 @@ pub(crate) fn missing_whitespace_after_keyword( let tok0_kind = tok0.kind(); let tok1_kind = tok1.kind(); - if tok0_kind.is_non_soft_keyword() + if tok0_kind.is_keyword() && !(tok0_kind.is_singleton() || matches!(tok0_kind, TokenKind::Async | TokenKind::Await) || tok0_kind == TokenKind::Except && tok1_kind == TokenKind::Star diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs index 933fe3bbfd3cdf..606972bcf0c38b 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs @@ -445,7 +445,7 @@ impl LogicalLinesBuilder { if matches!(kind, TokenKind::Comma | TokenKind::Semi | TokenKind::Colon) { line.flags.insert(TokenFlags::PUNCTUATION); - } else if kind.is_non_soft_keyword() { + } else if kind.is_keyword() { line.flags.insert(TokenFlags::KEYWORD); } diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_keywords.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_keywords.rs index 6914a386cb0830..365060f41d0e5e 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_keywords.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_keywords.rs @@ -127,8 +127,8 @@ pub(crate) fn whitespace_around_keywords(line: &LogicalLine, context: &mut Logic let mut after_keyword = false; for token in line.tokens() { - let is_non_soft_keyword = token.kind().is_non_soft_keyword(); - if is_non_soft_keyword { + let is_keyword = token.kind().is_keyword(); + if is_keyword { if !after_keyword { match line.leading_whitespace(token) { (Whitespace::Tab, offset) => { @@ -184,6 +184,6 @@ pub(crate) fn whitespace_around_keywords(line: &LogicalLine, context: &mut Logic } } - after_keyword = is_non_soft_keyword; + after_keyword = is_keyword; } } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E271_E27.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E271_E27.py.snap index 769525c450820c..328e086bcc436a 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E271_E27.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E271_E27.py.snap @@ -190,4 +190,22 @@ E27.py:35:14: E271 [*] Multiple spaces after keyword 37 37 | from w import(e, f) 38 38 | #: E275 +E27.py:70:5: E271 [*] Multiple spaces after keyword + | +69 | #: E271 +70 | type Number = int + | ^^ E271 +71 | +72 | #: E273 + | + = help: Replace with single space +ℹ Safe fix +67 67 | # Soft keywords +68 68 | +69 69 | #: E271 +70 |-type Number = int + 70 |+type Number = int +71 71 | +72 72 | #: E273 +73 73 | type Number = int diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E273_E27.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E273_E27.py.snap index 0923ce5e3f9c38..093022d930f226 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E273_E27.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E273_E27.py.snap @@ -106,4 +106,22 @@ E27.py:30:10: E273 [*] Tab after keyword 32 32 | from u import (a, b) 33 33 | from v import c, d +E27.py:73:5: E273 [*] Tab after keyword + | +72 | #: E273 +73 | type Number = int + | ^^^^ E273 +74 | +75 | #: E275 + | + = help: Replace with single space +ℹ Safe fix +70 70 | type Number = int +71 71 | +72 72 | #: E273 +73 |-type Number = int + 73 |+type Number = int +74 74 | +75 75 | #: E275 +76 76 | match(foo): diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap index 835d0d645f50fb..ab74afb69f5068 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap @@ -106,4 +106,39 @@ E27.py:54:5: E275 [*] Missing whitespace after keyword 56 56 | def f(): 57 57 | print((yield)) +E27.py:76:1: E275 [*] Missing whitespace after keyword + | +75 | #: E275 +76 | match(foo): + | ^^^^^ E275 +77 | case(1): +78 | pass + | + = help: Added missing whitespace after keyword + +ℹ Safe fix +73 73 | type Number = int +74 74 | +75 75 | #: E275 +76 |-match(foo): + 76 |+match (foo): +77 77 | case(1): +78 78 | pass +E27.py:77:5: E275 [*] Missing whitespace after keyword + | +75 | #: E275 +76 | match(foo): +77 | case(1): + | ^^^^ E275 +78 | pass + | + = help: Added missing whitespace after keyword + +ℹ Safe fix +74 74 | +75 75 | #: E275 +76 76 | match(foo): +77 |- case(1): + 77 |+ case (1): +78 78 | pass