Skip to content

Commit 7b4a73d

Browse files
authored
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.
1 parent 91af5a4 commit 7b4a73d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,9 @@
153153

154154
#: E203:1:13
155155
ham[lower + 1 :, "columnname"]
156+
157+
#: Okay
158+
f"{ham[lower +1 :, "columnname"]}"
159+
160+
#: E203:1:13
161+
f"{ham[lower + 1 :, "columnname"]}"

crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin
137137
match kind {
138138
TokenKind::FStringStart => fstrings += 1,
139139
TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1),
140-
TokenKind::Lsqb if fstrings == 0 => {
140+
TokenKind::Lsqb => {
141141
brackets.push(kind);
142142
}
143-
TokenKind::Rsqb if fstrings == 0 => {
143+
TokenKind::Rsqb => {
144144
brackets.pop();
145145
}
146-
TokenKind::Lbrace if fstrings == 0 => {
146+
TokenKind::Lbrace => {
147147
brackets.push(kind);
148148
}
149-
TokenKind::Rbrace if fstrings == 0 => {
149+
TokenKind::Rbrace => {
150150
brackets.pop();
151151
}
152152
_ => {}

crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ E20.py:155:14: E203 [*] Whitespace before ':'
251251
154 | #: E203:1:13
252252
155 | ham[lower + 1 :, "columnname"]
253253
| ^^ E203
254+
156 |
255+
157 | #: Okay
254256
|
255257
= help: Remove whitespace before ':'
256258

@@ -260,3 +262,21 @@ E20.py:155:14: E203 [*] Whitespace before ':'
260262
154 154 | #: E203:1:13
261263
155 |-ham[lower + 1 :, "columnname"]
262264
155 |+ham[lower + 1:, "columnname"]
265+
156 156 |
266+
157 157 | #: Okay
267+
158 158 | f"{ham[lower +1 :, "columnname"]}"
268+
269+
E20.py:161:17: E203 [*] Whitespace before ':'
270+
|
271+
160 | #: E203:1:13
272+
161 | f"{ham[lower + 1 :, "columnname"]}"
273+
| ^^ E203
274+
|
275+
= help: Remove whitespace before ':'
276+
277+
Safe fix
278+
158 158 | f"{ham[lower +1 :, "columnname"]}"
279+
159 159 |
280+
160 160 | #: E203:1:13
281+
161 |-f"{ham[lower + 1 :, "columnname"]}"
282+
161 |+f"{ham[lower + 1:, "columnname"]}"

0 commit comments

Comments
 (0)