From 5d0fbe2b6ddcf5a2d00a668a45b7d2182b4efd58 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Thu, 8 Aug 2024 20:17:28 -0500 Subject: [PATCH 1/3] update rule, fixture, and snapshot --- crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py | 4 +++- .../resources/test/fixtures/ruff/RUF031_prefer_parens.py | 5 ++++- .../rules/incorrectly_parenthesized_tuple_in_subscript.rs | 5 ++++- .../ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap | 3 +++ ...rules__ruff__tests__prefer_parentheses_getitem_tuple.snap | 2 ++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py index 89e06f6bd4e20..4000930038b49 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py @@ -26,4 +26,6 @@ d[1,] d[(1,)] -d[()] # empty tuples should be ignored \ No newline at end of file +d[()] # empty tuples should be ignored +d[:,] # slices in the subscript lead to syntax error if parens are added +d[1,2,:] \ No newline at end of file diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py index f3d701ae0df3a..f546515a743fe 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py @@ -25,4 +25,7 @@ ] = self._extract_raw_features_from_token d[1,] d[(1,)] -d[()] # empty tuples should be ignored \ No newline at end of file +d[()] # empty tuples should be ignored + +d[:,] # slices in the subscript lead to syntax error if parens are added +d[1,2,:] \ No newline at end of file diff --git a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs index 01e8638c0d220..ce6d9c50801bf 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs @@ -1,6 +1,6 @@ use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::ExprSubscript; +use ruff_python_ast::{Expr, ExprSubscript}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; @@ -64,6 +64,9 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip if tuple_subscript.parenthesized == prefer_parentheses || tuple_subscript.elts.is_empty() { return; } + if prefer_parentheses & tuple_subscript.elts.iter().any(Expr::is_slice_expr) { + return; + } let locator = checker.locator(); let source_range = subscript.slice.range(); let new_source = if prefer_parentheses { diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap index 2c9d230802a60..4dbad809a6b3d 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap @@ -156,6 +156,7 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts. 28 | d[(1,)] | ^^^^ RUF031 29 | d[()] # empty tuples should be ignored +30 | d[:,] # slices in the subscript lead to syntax error if parens are added | = help: Remove the parentheses. @@ -166,3 +167,5 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts. 28 |-d[(1,)] 28 |+d[1,] 29 29 | d[()] # empty tuples should be ignored +30 30 | d[:,] # slices in the subscript lead to syntax error if parens are added +31 31 | d[1,2,:] diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__prefer_parentheses_getitem_tuple.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__prefer_parentheses_getitem_tuple.snap index 9f776e10124a7..9af8d5e20bb47 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__prefer_parentheses_getitem_tuple.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__prefer_parentheses_getitem_tuple.snap @@ -129,3 +129,5 @@ RUF031_prefer_parens.py:26:3: RUF031 [*] Use parentheses for tuples in subscript 27 26 | d[(1,)] 27 |+d[(1,)] 28 28 | d[()] # empty tuples should be ignored +29 29 | +30 30 | d[:,] # slices in the subscript lead to syntax error if parens are added From 1f443c29da29050b12e0c41fe6d1d2aa7dc21ddc Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Thu, 8 Aug 2024 20:23:31 -0500 Subject: [PATCH 2/3] add clarifying comment --- .../ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs index ce6d9c50801bf..e05477d1b13ce 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs @@ -64,6 +64,7 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip if tuple_subscript.parenthesized == prefer_parentheses || tuple_subscript.elts.is_empty() { return; } + // Adding parentheses in the presence of a slice leads to a syntax error. if prefer_parentheses & tuple_subscript.elts.iter().any(Expr::is_slice_expr) { return; } From 497fd770770740fe35d8816b3a777e2e9b65a7a1 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 9 Aug 2024 10:18:45 +0100 Subject: [PATCH 3/3] Update crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs --- .../ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs index e05477d1b13ce..473a0e2f5f8c2 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs @@ -65,7 +65,7 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip return; } // Adding parentheses in the presence of a slice leads to a syntax error. - if prefer_parentheses & tuple_subscript.elts.iter().any(Expr::is_slice_expr) { + if prefer_parentheses && tuple_subscript.elts.iter().any(Expr::is_slice_expr) { return; } let locator = checker.locator();