mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Avoid RUF007
fixes for more than two arguments (#3654)
This commit is contained in:
parent
41e38ffa98
commit
5eae3fbbfb
4 changed files with 131 additions and 73 deletions
|
@ -1,5 +1,6 @@
|
||||||
input = [1, 2, 3]
|
input = [1, 2, 3]
|
||||||
otherInput = [2, 3, 4]
|
otherInput = [2, 3, 4]
|
||||||
|
foo = [1, 2, 3, 4]
|
||||||
|
|
||||||
# OK
|
# OK
|
||||||
zip(input, otherInput) # different inputs
|
zip(input, otherInput) # different inputs
|
||||||
|
@ -8,6 +9,10 @@ zip(input, input[2:]) # not successive
|
||||||
zip(input[:-1], input[2:]) # not successive
|
zip(input[:-1], input[2:]) # not successive
|
||||||
list(zip(input, otherInput)) # nested call
|
list(zip(input, otherInput)) # nested call
|
||||||
zip(input, input[1::2]) # not successive
|
zip(input, input[1::2]) # not successive
|
||||||
|
zip(foo[:-1], foo[1:], foo, strict=False) # more than 2 inputs
|
||||||
|
zip(foo[:-1], foo[1:], foo, strict=True) # more than 2 inputs
|
||||||
|
zip(foo[:-1], foo[1:], strict=True) # use strict
|
||||||
|
zip(foo[:-1], foo[1:], strict=bool(foo)) # use strict
|
||||||
|
|
||||||
# Errors
|
# Errors
|
||||||
zip(input, input[1:])
|
zip(input, input[1:])
|
||||||
|
@ -17,3 +22,4 @@ zip(input[1:], input[2:])
|
||||||
zip(input[1:-1], input[2:])
|
zip(input[1:-1], input[2:])
|
||||||
list(zip(input, input[1:]))
|
list(zip(input, input[1:]))
|
||||||
list(zip(input[:-1], input[1:]))
|
list(zip(input[:-1], input[1:]))
|
||||||
|
zip(foo[:-1], foo[1:], strict=False)
|
||||||
|
|
|
@ -2864,7 +2864,7 @@ where
|
||||||
|
|
||||||
if self.settings.rules.enabled(Rule::PairwiseOverZipped) {
|
if self.settings.rules.enabled(Rule::PairwiseOverZipped) {
|
||||||
if self.settings.target_version >= PythonVersion::Py310 {
|
if self.settings.target_version >= PythonVersion::Py310 {
|
||||||
ruff::rules::pairwise_over_zipped(self, func, args);
|
ruff::rules::pairwise_over_zipped(self, func, args, keywords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use num_traits::ToPrimitive;
|
use num_traits::ToPrimitive;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Unaryop};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, KeywordData, Unaryop};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
@ -88,14 +88,53 @@ fn to_bound(expr: &Expr) -> Option<i64> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RUF007
|
/// RUF007
|
||||||
pub fn pairwise_over_zipped(checker: &mut Checker, func: &Expr, args: &[Expr]) {
|
pub fn pairwise_over_zipped(
|
||||||
|
checker: &mut Checker,
|
||||||
|
func: &Expr,
|
||||||
|
args: &[Expr],
|
||||||
|
keywords: &[Keyword],
|
||||||
|
) {
|
||||||
let ExprKind::Name { id, .. } = &func.node else {
|
let ExprKind::Name { id, .. } = &func.node else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if !(args.len() > 1 && id == "zip" && checker.ctx.is_builtin(id)) {
|
// Require exactly two positional arguments.
|
||||||
|
if args.len() != 2 {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// Allow `strict=False`, but no other keyword arguments.
|
||||||
|
if keywords.iter().any(|keyword| {
|
||||||
|
let KeywordData {
|
||||||
|
arg: Some(arg),
|
||||||
|
value,
|
||||||
|
} = &keyword.node else {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
if arg != "strict" {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if matches!(
|
||||||
|
value.node,
|
||||||
|
ExprKind::Constant {
|
||||||
|
value: Constant::Bool(false),
|
||||||
|
..
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Require the function to be the builtin `zip`.
|
||||||
|
if id != "zip" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if !checker.ctx.is_builtin(id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Allow the first argument to be a `Name` or `Subscript`.
|
// Allow the first argument to be a `Name` or `Subscript`.
|
||||||
let Some(first_arg_info) = ({
|
let Some(first_arg_info) = ({
|
||||||
|
|
|
@ -2,71 +2,6 @@
|
||||||
source: crates/ruff/src/rules/ruff/mod.rs
|
source: crates/ruff/src/rules/ruff/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
- kind:
|
|
||||||
name: PairwiseOverZipped
|
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
|
||||||
suggestion: ~
|
|
||||||
fixable: false
|
|
||||||
location:
|
|
||||||
row: 13
|
|
||||||
column: 0
|
|
||||||
end_location:
|
|
||||||
row: 13
|
|
||||||
column: 3
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
|
||||||
name: PairwiseOverZipped
|
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
|
||||||
suggestion: ~
|
|
||||||
fixable: false
|
|
||||||
location:
|
|
||||||
row: 14
|
|
||||||
column: 0
|
|
||||||
end_location:
|
|
||||||
row: 14
|
|
||||||
column: 3
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
|
||||||
name: PairwiseOverZipped
|
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
|
||||||
suggestion: ~
|
|
||||||
fixable: false
|
|
||||||
location:
|
|
||||||
row: 15
|
|
||||||
column: 0
|
|
||||||
end_location:
|
|
||||||
row: 15
|
|
||||||
column: 3
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
|
||||||
name: PairwiseOverZipped
|
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
|
||||||
suggestion: ~
|
|
||||||
fixable: false
|
|
||||||
location:
|
|
||||||
row: 16
|
|
||||||
column: 0
|
|
||||||
end_location:
|
|
||||||
row: 16
|
|
||||||
column: 3
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
|
||||||
name: PairwiseOverZipped
|
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
|
||||||
suggestion: ~
|
|
||||||
fixable: false
|
|
||||||
location:
|
|
||||||
row: 17
|
|
||||||
column: 0
|
|
||||||
end_location:
|
|
||||||
row: 17
|
|
||||||
column: 3
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
- kind:
|
||||||
name: PairwiseOverZipped
|
name: PairwiseOverZipped
|
||||||
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
@ -74,10 +9,10 @@ expression: diagnostics
|
||||||
fixable: false
|
fixable: false
|
||||||
location:
|
location:
|
||||||
row: 18
|
row: 18
|
||||||
column: 5
|
column: 0
|
||||||
end_location:
|
end_location:
|
||||||
row: 18
|
row: 18
|
||||||
column: 8
|
column: 3
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
|
@ -87,10 +22,88 @@ expression: diagnostics
|
||||||
fixable: false
|
fixable: false
|
||||||
location:
|
location:
|
||||||
row: 19
|
row: 19
|
||||||
column: 5
|
column: 0
|
||||||
end_location:
|
end_location:
|
||||||
row: 19
|
row: 19
|
||||||
|
column: 3
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 20
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 20
|
||||||
|
column: 3
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 21
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 21
|
||||||
|
column: 3
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 22
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 22
|
||||||
|
column: 3
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 23
|
||||||
|
column: 5
|
||||||
|
end_location:
|
||||||
|
row: 23
|
||||||
column: 8
|
column: 8
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 24
|
||||||
|
column: 5
|
||||||
|
end_location:
|
||||||
|
row: 24
|
||||||
|
column: 8
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: PairwiseOverZipped
|
||||||
|
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
|
||||||
|
suggestion: ~
|
||||||
|
fixable: false
|
||||||
|
location:
|
||||||
|
row: 25
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 25
|
||||||
|
column: 3
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue