Avoid flagging nested f-strings (#1516)

This commit is contained in:
Charlie Marsh 2022-12-31 13:41:21 -05:00 committed by GitHub
parent bfdf972a5d
commit 9ba17fbf92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 14 deletions

View File

@ -1,6 +1,8 @@
# OK
a = "abc" a = "abc"
b = f"ghi{'jkl'}" b = f"ghi{'jkl'}"
# Errors
c = f"def" c = f"def"
d = f"def" + "ghi" d = f"def" + "ghi"
e = ( e = (
@ -8,5 +10,17 @@ e = (
"ghi" "ghi"
) )
# OK
g = f"ghi{123:{45}}" g = f"ghi{123:{45}}"
# Error
h = "x" "y" f"z" h = "x" "y" f"z"
v = 23.234234
# OK
f"{v:0.2f}"
# OK (false negatives)
f"{v:{f'0.2f'}}"
f"{f''}"

View File

@ -91,6 +91,7 @@ pub struct Checker<'a> {
in_type_definition: bool, in_type_definition: bool,
in_deferred_string_type_definition: bool, in_deferred_string_type_definition: bool,
in_deferred_type_definition: bool, in_deferred_type_definition: bool,
in_f_string: bool,
in_literal: bool, in_literal: bool,
in_subscript: bool, in_subscript: bool,
seen_import_boundary: bool, seen_import_boundary: bool,
@ -147,6 +148,7 @@ impl<'a> Checker<'a> {
in_type_definition: false, in_type_definition: false,
in_deferred_string_type_definition: false, in_deferred_string_type_definition: false,
in_deferred_type_definition: false, in_deferred_type_definition: false,
in_f_string: false,
in_literal: false, in_literal: false,
in_subscript: false, in_subscript: false,
seen_import_boundary: false, seen_import_boundary: false,
@ -1466,6 +1468,7 @@ where
self.push_expr(expr); self.push_expr(expr);
let prev_in_f_string = self.in_f_string;
let prev_in_literal = self.in_literal; let prev_in_literal = self.in_literal;
let prev_in_type_definition = self.in_type_definition; let prev_in_type_definition = self.in_type_definition;
@ -2151,7 +2154,9 @@ where
} }
} }
ExprKind::JoinedStr { values } => { ExprKind::JoinedStr { values } => {
if self.settings.enabled.contains(&CheckCode::F541) { // Conversion flags are parsed as f-strings without placeholders, so skip
// nested f-strings, which would lead to false positives.
if !self.in_f_string && self.settings.enabled.contains(&CheckCode::F541) {
if !values if !values
.iter() .iter()
.any(|value| matches!(value.node, ExprKind::FormattedValue { .. })) .any(|value| matches!(value.node, ExprKind::FormattedValue { .. }))
@ -2654,6 +2659,11 @@ where
} }
self.in_subscript = prev_in_subscript; self.in_subscript = prev_in_subscript;
} }
ExprKind::JoinedStr { .. } => {
self.in_f_string = true;
visitor::walk_expr(self, expr);
self.in_f_string = prev_in_f_string;
}
_ => visitor::walk_expr(self, expr), _ => visitor::walk_expr(self, expr),
} }
@ -2671,6 +2681,7 @@ where
self.in_type_definition = prev_in_type_definition; self.in_type_definition = prev_in_type_definition;
self.in_literal = prev_in_literal; self.in_literal = prev_in_literal;
self.in_f_string = prev_in_f_string;
self.pop_expr(); self.pop_expr();
} }

View File

@ -4,19 +4,10 @@ expression: checks
--- ---
- kind: FStringMissingPlaceholders - kind: FStringMissingPlaceholders
location: location:
row: 4 row: 6
column: 4 column: 4
end_location: end_location:
row: 4 row: 6
column: 10
fix: ~
parent: ~
- kind: FStringMissingPlaceholders
location:
row: 5
column: 4
end_location:
row: 5
column: 10 column: 10
fix: ~ fix: ~
parent: ~ parent: ~
@ -31,10 +22,19 @@ expression: checks
parent: ~ parent: ~
- kind: FStringMissingPlaceholders - kind: FStringMissingPlaceholders
location: location:
row: 12 row: 9
column: 4 column: 4
end_location: end_location:
row: 12 row: 9
column: 10
fix: ~
parent: ~
- kind: FStringMissingPlaceholders
location:
row: 17
column: 4
end_location:
row: 17
column: 16 column: 16
fix: ~ fix: ~
parent: ~ parent: ~