Skip to content

Commit

Permalink
Fix E203 false positive for slices in format strings (astral-sh#10280)
Browse files Browse the repository at this point in the history
## 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 astral-sh#10278

## Test Plan

~Still needs a test.~ Test case added for this example.
  • Loading branch information
sciyoshi authored and nkxxll committed Mar 10, 2024
1 parent 9543db1 commit fc6b40b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}"
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
_ => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ':'

Expand All @@ -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"]}"

0 comments on commit fc6b40b

Please sign in to comment.