Skip to content

Commit

Permalink
Fix F507 false positive (astral-sh#5986)
Browse files Browse the repository at this point in the history
## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

F507 should not be raised when the right-hand side value is a non-tuple
object.

```python
'%s' % (1, 2, 3)  # throws
'%s' % [1, 2, 3]  # doesn't throw
'%s' % {1, 2, 3}  # doesn't throw
```
  • Loading branch information
harupy authored Jul 22, 2023
1 parent ed7d2b8 commit 97e31ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F50x.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
'%s %s' % (*a,)
k = {}
'%(k)s' % {**k}
'%s' % [1, 2, 3]
'%s' % {1, 2, 3}
95 changes: 46 additions & 49 deletions crates/ruff/src/rules/pyflakes/rules/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,40 +634,42 @@ pub(crate) fn percent_format_missing_arguments(
return;
}

if let Expr::Dict(ast::ExprDict { keys, .. }) = &right {
if keys.iter().any(Option::is_none) {
return; // contains **x splat
}
let Expr::Dict(ast::ExprDict { keys, .. }) = &right else {
return;
};

let mut keywords = FxHashSet::default();
for key in keys.iter().flatten() {
match key {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
..
}) => {
keywords.insert(value);
}
_ => {
return; // Dynamic keys present
}
if keys.iter().any(Option::is_none) {
return; // contains **x splat
}

let mut keywords = FxHashSet::default();
for key in keys.iter().flatten() {
match key {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
..
}) => {
keywords.insert(value);
}
_ => {
return; // Dynamic keys present
}
}
}

let missing: Vec<&String> = summary
.keywords
.iter()
.filter(|k| !keywords.contains(k))
.collect();
let missing: Vec<&String> = summary
.keywords
.iter()
.filter(|k| !keywords.contains(k))
.collect();

if !missing.is_empty() {
checker.diagnostics.push(Diagnostic::new(
PercentFormatMissingArgument {
missing: missing.iter().map(|&s| s.clone()).collect(),
},
location,
));
}
if !missing.is_empty() {
checker.diagnostics.push(Diagnostic::new(
PercentFormatMissingArgument {
missing: missing.iter().map(|&s| s.clone()).collect(),
},
location,
));
}
}

Expand Down Expand Up @@ -696,29 +698,24 @@ pub(crate) fn percent_format_positional_count_mismatch(
return;
}

match right {
Expr::List(ast::ExprList { elts, .. })
| Expr::Tuple(ast::ExprTuple { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. }) => {
let mut found = 0;
for elt in elts {
if let Expr::Starred(_) = &elt {
return;
}
found += 1;
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = right {
let mut found = 0;
for elt in elts {
if elt.is_starred_expr() {
return;
}
found += 1;
}

if found != summary.num_positional {
checker.diagnostics.push(Diagnostic::new(
PercentFormatPositionalCountMismatch {
wanted: summary.num_positional,
got: found,
},
location,
));
}
if found != summary.num_positional {
checker.diagnostics.push(Diagnostic::new(
PercentFormatPositionalCountMismatch {
wanted: summary.num_positional,
got: found,
},
location,
));
}
_ => {}
}
}

Expand Down

0 comments on commit 97e31ca

Please sign in to comment.