[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
This commit is contained in:
Dylan 2025-02-10 11:30:07 -06:00 committed by GitHub
parent a4c8c49ac2
commit f30fac6326
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 0 deletions

View file

@ -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]

View file

@ -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]

View file

@ -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;