Skip to content

Commit

Permalink
[ruff] Skip singleton starred expressions for `incorrectly-parenthe…
Browse files Browse the repository at this point in the history
…sized-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
  • Loading branch information
dylwil3 authored Feb 10, 2025
1 parent a4c8c49 commit f30fac6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@
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]
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@
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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f30fac6

Please sign in to comment.