Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix E251 false positive inside f-strings #7894

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E25.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ def add(a: int = _default(name='f')):
f"{a:=1}"
f"{foo(a=1)}"
f"normal {f"{a=}"} normal"

# Okay as the `=` is used inside a f-string...
print(f"{foo = }")
# ...but then it creates false negatives for now
print(f"{foo(a = 1)}")
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub(crate) fn whitespace_around_named_parameter_equals(
context: &mut LogicalLinesContext,
) {
let mut parens = 0u32;
let mut fstrings = 0u32;
let mut annotated_func_arg = false;
let mut prev_end = TextSize::default();

Expand All @@ -108,6 +109,8 @@ pub(crate) fn whitespace_around_named_parameter_equals(
}

match kind {
TokenKind::FStringStart => fstrings += 1,
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1),
TokenKind::Lpar | TokenKind::Lsqb => {
parens = parens.saturating_add(1);
}
Expand All @@ -124,7 +127,7 @@ pub(crate) fn whitespace_around_named_parameter_equals(
TokenKind::Comma if parens == 1 => {
annotated_func_arg = false;
}
TokenKind::Equal if parens > 0 => {
TokenKind::Equal if parens > 0 && fstrings == 0 => {
if annotated_func_arg && parens == 1 {
let start = token.start();
if start == prev_end && prev_end != TextSize::new(0) {
Expand Down
Loading