Avoid E203 for f-string debug expression (#12024)

## 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.
This commit is contained in:
Dhruv Manilawala 2024-06-25 15:00:31 +05:30 committed by GitHub
parent 7109214b57
commit 2853751344
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 0 deletions

View file

@ -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),

View file

@ -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 ':'
@ -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}"