Skip to content

Commit f30fac6

Browse files
authored
[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
1 parent a4c8c49 commit f30fac6

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py

+4
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@
4242
type Y = typing.Literal[1, 2]
4343
Z: typing.TypeAlias = dict[int, int]
4444
class Foo(dict[str, int]): pass
45+
46+
# Skip tuples of length one that are single-starred expressions
47+
# https://github.com/astral-sh/ruff/issues/16077
48+
d[*x]

crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py

+4
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@
4242
type Y = typing.Literal[1, 2]
4343
Z: typing.TypeAlias = dict[int, int]
4444
class Foo(dict[str, int]): pass
45+
46+
# Skip tuples of length one that are single-starred expressions
47+
# https://github.com/astral-sh/ruff/issues/16077
48+
d[*x]

crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &Checker, subscript: &
7373
return;
7474
}
7575

76+
// We should not handle single starred expressions
77+
// (regardless of `prefer_parentheses`)
78+
if matches!(&tuple_subscript.elts[..], &[Expr::Starred(_)]) {
79+
return;
80+
}
81+
7682
// Adding parentheses in the presence of a slice leads to a syntax error.
7783
if prefer_parentheses && tuple_subscript.iter().any(Expr::is_slice_expr) {
7884
return;

0 commit comments

Comments
 (0)