From fc6b40b48ab7311261bf5a2236ae0627a2755045 Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Thu, 7 Mar 2024 17:09:05 -0500 Subject: [PATCH] Fix E203 false positive for slices in format strings (#10280) ## Summary The code later in this file that checks for slices relies on the stack of brackets to determine the position. I'm not sure why format strings were being excluded from this, but the tests still pass with these match guards removed. Closes #10278 ## Test Plan ~Still needs a test.~ Test case added for this example. --- .../test/fixtures/pycodestyle/E20.py | 6 ++++++ .../logical_lines/extraneous_whitespace.rs | 8 ++++---- ...ules__pycodestyle__tests__E203_E20.py.snap | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py index 2817f2ef9b6f8..01d49e2930c51 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py @@ -153,3 +153,9 @@ #: E203:1:13 ham[lower + 1 :, "columnname"] + +#: Okay +f"{ham[lower +1 :, "columnname"]}" + +#: E203:1:13 +f"{ham[lower + 1 :, "columnname"]}" diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs index 3f568cf99dc06..216592973e67d 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs @@ -137,16 +137,16 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin match kind { TokenKind::FStringStart => fstrings += 1, TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1), - TokenKind::Lsqb if fstrings == 0 => { + TokenKind::Lsqb => { brackets.push(kind); } - TokenKind::Rsqb if fstrings == 0 => { + TokenKind::Rsqb => { brackets.pop(); } - TokenKind::Lbrace if fstrings == 0 => { + TokenKind::Lbrace => { brackets.push(kind); } - TokenKind::Rbrace if fstrings == 0 => { + TokenKind::Rbrace => { brackets.pop(); } _ => {} diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap index 01c7ebc0713f6..c70c11d79c8ba 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap @@ -251,6 +251,8 @@ E20.py:155:14: E203 [*] Whitespace before ':' 154 | #: E203:1:13 155 | ham[lower + 1 :, "columnname"] | ^^ E203 +156 | +157 | #: Okay | = help: Remove whitespace before ':' @@ -260,3 +262,21 @@ E20.py:155:14: E203 [*] Whitespace before ':' 154 154 | #: E203:1:13 155 |-ham[lower + 1 :, "columnname"] 155 |+ham[lower + 1:, "columnname"] +156 156 | +157 157 | #: Okay +158 158 | f"{ham[lower +1 :, "columnname"]}" + +E20.py:161:17: E203 [*] Whitespace before ':' + | +160 | #: E203:1:13 +161 | f"{ham[lower + 1 :, "columnname"]}" + | ^^ E203 + | + = help: Remove whitespace before ':' + +ℹ Safe fix +158 158 | f"{ham[lower +1 :, "columnname"]}" +159 159 | +160 160 | #: E203:1:13 +161 |-f"{ham[lower + 1 :, "columnname"]}" + 161 |+f"{ham[lower + 1:, "columnname"]}"