[ruff] Skip singleton starred expressions for incorrectly-parenthesized-tuple-in-subscript (RUF031) (#16083)
Some checks are pending
CI / cargo test (linux) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

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;