From f30fac632685c1edd200005d110cf407992bd69a Mon Sep 17 00:00:00 2001 From: Dylan <53534755+dylwil3@users.noreply.github.com> Date: Mon, 10 Feb 2025 11:30:07 -0600 Subject: [PATCH] [`ruff`] Skip singleton starred expressions for `incorrectly-parenthesized-tuple-in-subscript` (`RUF031`) (#16083) The index in subscript access like `d[*y]` will not be linted or autofixed with parentheses, even when `lint.ruff.parenthesize-tuple-in-subscript = true`. Closes #16077 --- crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py | 4 ++++ .../resources/test/fixtures/ruff/RUF031_prefer_parens.py | 4 ++++ .../rules/incorrectly_parenthesized_tuple_in_subscript.rs | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py index e784125680..9e869b22db 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py @@ -42,3 +42,7 @@ import typing type Y = typing.Literal[1, 2] Z: typing.TypeAlias = dict[int, int] class Foo(dict[str, int]): pass + +# Skip tuples of length one that are single-starred expressions +# https://github.com/astral-sh/ruff/issues/16077 +d[*x] 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 0d9afff348..3c4231c140 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 @@ -42,3 +42,7 @@ import typing type Y = typing.Literal[1, 2] Z: typing.TypeAlias = dict[int, int] class Foo(dict[str, int]): pass + +# Skip tuples of length one that are single-starred expressions +# https://github.com/astral-sh/ruff/issues/16077 +d[*x] 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 0242c1717f..9764a8155c 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 @@ -73,6 +73,12 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &Checker, subscript: & return; } + // We should not handle single starred expressions + // (regardless of `prefer_parentheses`) + if matches!(&tuple_subscript.elts[..], &[Expr::Starred(_)]) { + return; + } + // Adding parentheses in the presence of a slice leads to a syntax error. if prefer_parentheses && tuple_subscript.iter().any(Expr::is_slice_expr) { return;