From 7741d43ae5e32a95ff62fb12744b055f7ac3f108 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 22 Mar 2023 13:03:43 -0400 Subject: [PATCH] Allow `pairwise` diagnostics for `zip(..., strict=True)` (#3669) --- .../resources/test/fixtures/ruff/RUF007.py | 4 +- crates/ruff/src/checkers/ast/mod.rs | 2 +- .../rules/ruff/rules/pairwise_over_zipped.rs | 34 +-------------- ...uff__tests__ruff_pairwise_over_zipped.snap | 42 +++++++++++++++---- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/ruff/RUF007.py b/crates/ruff/resources/test/fixtures/ruff/RUF007.py index 5b1fb845522e0..b5890e3e71d83 100644 --- a/crates/ruff/resources/test/fixtures/ruff/RUF007.py +++ b/crates/ruff/resources/test/fixtures/ruff/RUF007.py @@ -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:]) @@ -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)) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 8b62f6dde68fa..c8fea9af15f0c 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -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); } } diff --git a/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs b/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs index bd034b606c781..8eb11d5074cdc 100644 --- a/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs +++ b/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs @@ -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}; @@ -88,12 +88,7 @@ fn to_bound(expr: &Expr) -> Option { } /// 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; }; @@ -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; diff --git a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruff_pairwise_over_zipped.snap b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruff_pairwise_over_zipped.snap index 39972631afd32..bdbda3c21433b 100644 --- a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruff_pairwise_over_zipped.snap +++ b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruff_pairwise_over_zipped.snap @@ -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" @@ -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: @@ -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: @@ -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: @@ -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: