Skip to content

Commit

Permalink
Avoid E203 for f-string debug expression (#12024)
Browse files Browse the repository at this point in the history
## Summary

This PR fixes a bug where Ruff would raise `E203` for f-string debug
expression. This isn't valid because whitespaces are important for debug
expressions.

fixes: #12023

## Test Plan

Add test case and make sure there are no snapshot changes.
  • Loading branch information
dhruvmanila committed Jun 25, 2024
1 parent 7109214 commit 2853751
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@

#: E203:1:13
f"{ham[lower + 1 :, "columnname"]}"

#: Okay: https://github.com/astral-sh/ruff/issues/12023
f"{x = :.2f}"
f"{(x) = :.2f}"
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin
}
}
} else {
if fstrings > 0
&& symbol == ':'
&& matches!(prev_token, Some(TokenKind::Equal))
{
// Avoid removing any whitespace for f-string debug expressions.
continue;
}
let mut diagnostic = Diagnostic::new(
WhitespaceBeforePunctuation { symbol },
TextRange::at(token.start() - offset, offset),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ E20.py:187:17: E203 [*] Whitespace before ':'
186 | #: E203:1:13
187 | f"{ham[lower + 1 :, "columnname"]}"
| ^^ E203
188 |
189 | #: Okay: https://github.com/astral-sh/ruff/issues/12023
|
= help: Remove whitespace before ':'

Expand All @@ -340,3 +342,6 @@ E20.py:187:17: E203 [*] Whitespace before ':'
186 186 | #: E203:1:13
187 |-f"{ham[lower + 1 :, "columnname"]}"
187 |+f"{ham[lower + 1:, "columnname"]}"
188 188 |
189 189 | #: Okay: https://github.com/astral-sh/ruff/issues/12023
190 190 | f"{x = :.2f}"

0 comments on commit 2853751

Please sign in to comment.