Skip to content

Commit

Permalink
Allow pairwise diagnostics for zip(..., strict=True) (#3669)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 22, 2023
1 parent 1b3e542 commit 7741d43
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/resources/test/fixtures/ruff/RUF007.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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
zip(input, input[1:])
Expand All @@ -22,4 +20,6 @@
zip(input[1:-1], input[2:])
list(zip(input, input[1:]))
list(zip(input[:-1], input[1:]))
zip(foo[:-1], foo[1:], strict=True)
zip(foo[:-1], foo[1:], strict=False)
zip(foo[:-1], foo[1:], strict=bool(foo))
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2858,7 +2858,7 @@ where

if self.settings.rules.enabled(Rule::PairwiseOverZipped) {
if self.settings.target_version >= PythonVersion::Py310 {
ruff::rules::pairwise_over_zipped(self, func, args, keywords);
ruff::rules::pairwise_over_zipped(self, func, args);
}
}

Expand Down
34 changes: 2 additions & 32 deletions crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num_traits::ToPrimitive;
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, KeywordData, Unaryop};
use rustpython_parser::ast::{Constant, Expr, ExprKind, Unaryop};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -88,12 +88,7 @@ fn to_bound(expr: &Expr) -> Option<i64> {
}

/// RUF007
pub fn pairwise_over_zipped(
checker: &mut Checker,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) {
pub fn pairwise_over_zipped(checker: &mut Checker, func: &Expr, args: &[Expr]) {
let ExprKind::Name { id, .. } = &func.node else {
return;
};
Expand All @@ -103,31 +98,6 @@ pub fn pairwise_over_zipped(
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@
source: crates/ruff/src/rules/ruff/mod.rs
expression: diagnostics
---
- 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:
name: PairwiseOverZipped
body: "Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs"
Expand Down Expand Up @@ -48,10 +74,10 @@ expression: diagnostics
fixable: false
location:
row: 21
column: 0
column: 5
end_location:
row: 21
column: 3
column: 8
fix: ~
parent: ~
- kind:
Expand All @@ -61,10 +87,10 @@ expression: diagnostics
fixable: false
location:
row: 22
column: 0
column: 5
end_location:
row: 22
column: 3
column: 8
fix: ~
parent: ~
- kind:
Expand All @@ -74,10 +100,10 @@ expression: diagnostics
fixable: false
location:
row: 23
column: 5
column: 0
end_location:
row: 23
column: 8
column: 3
fix: ~
parent: ~
- kind:
Expand All @@ -87,10 +113,10 @@ expression: diagnostics
fixable: false
location:
row: 24
column: 5
column: 0
end_location:
row: 24
column: 8
column: 3
fix: ~
parent: ~
- kind:
Expand Down

0 comments on commit 7741d43

Please sign in to comment.