Skip to content

Commit

Permalink
Add visit_format_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Jan 1, 2023
1 parent e3867b1 commit a3828f0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions a.py
@@ -0,0 +1 @@
f"{x:0.2f}"
5 changes: 4 additions & 1 deletion src/ast/visitor.rs
Expand Up @@ -38,6 +38,9 @@ pub trait Visitor<'a> {
fn visit_excepthandler(&mut self, excepthandler: &'a Excepthandler) {
walk_excepthandler(self, excepthandler);
}
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
walk_expr(self, format_spec);
}
fn visit_arguments(&mut self, arguments: &'a Arguments) {
walk_arguments(self, arguments);
}
Expand Down Expand Up @@ -387,7 +390,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
} => {
visitor.visit_expr(value);
if let Some(expr) = format_spec {
visitor.visit_expr(expr);
visitor.visit_format_spec(expr);
}
}
ExprKind::JoinedStr { values } => {
Expand Down
21 changes: 12 additions & 9 deletions src/checkers/ast.rs
Expand Up @@ -91,7 +91,6 @@ pub struct Checker<'a> {
in_type_definition: bool,
in_deferred_string_type_definition: bool,
in_deferred_type_definition: bool,
in_f_string: bool,
in_literal: bool,
in_subscript: bool,
seen_import_boundary: bool,
Expand Down Expand Up @@ -148,7 +147,6 @@ impl<'a> Checker<'a> {
in_type_definition: false,
in_deferred_string_type_definition: false,
in_deferred_type_definition: false,
in_f_string: false,
in_literal: false,
in_subscript: false,
seen_import_boundary: false,
Expand Down Expand Up @@ -1486,7 +1484,6 @@ where

self.push_expr(expr);

let prev_in_f_string = self.in_f_string;
let prev_in_literal = self.in_literal;
let prev_in_type_definition = self.in_type_definition;

Expand Down Expand Up @@ -2176,9 +2173,7 @@ where
}
}
ExprKind::JoinedStr { values } => {
// 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 self.settings.enabled.contains(&CheckCode::F541) {
if !values
.iter()
.any(|value| matches!(value.node, ExprKind::FormattedValue { .. }))
Expand Down Expand Up @@ -2682,9 +2677,7 @@ where
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),
}
Expand All @@ -2703,7 +2696,6 @@ where

self.in_type_definition = prev_in_type_definition;
self.in_literal = prev_in_literal;
self.in_f_string = prev_in_f_string;

self.pop_expr();
}
Expand Down Expand Up @@ -2807,6 +2799,17 @@ where
}
}

fn visit_format_spec(&mut self, format_spec: &'b Expr) {
match &format_spec.node {
ExprKind::JoinedStr { values } => {
for value in values {
self.visit_expr(value);
}
}
_ => unreachable!("Unexpected expression for format_spec"),
}
}

fn visit_arguments(&mut self, arguments: &'b Arguments) {
if self.settings.enabled.contains(&CheckCode::B006) {
flake8_bugbear::plugins::mutable_argument_default(self, arguments);
Expand Down
18 changes: 18 additions & 0 deletions src/pyflakes/snapshots/ruff__pyflakes__tests__F541_F541.py.snap
Expand Up @@ -38,4 +38,22 @@ expression: checks
column: 16
fix: ~
parent: ~
- kind: FStringMissingPlaceholders
location:
row: 25
column: 6
end_location:
row: 25
column: 13
fix: ~
parent: ~
- kind: FStringMissingPlaceholders
location:
row: 26
column: 3
end_location:
row: 26
column: 6
fix: ~
parent: ~

0 comments on commit a3828f0

Please sign in to comment.