Skip to content

Commit f577e03

Browse files
authored
[ruff] Ignore empty tuples for incorrectly-parenthesized-tuple-in-subscript (RUF031) (#12749)
1 parent f537335 commit f577e03

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626

2727
d[1,]
2828
d[(1,)]
29+
d[()] # empty tuples should be ignored

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
] = self._extract_raw_features_from_token
2626
d[1,]
2727
d[(1,)]
28+
d[()] # empty tuples should be ignored

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ruff_text_size::Ranged;
66
use crate::checkers::ast::Checker;
77

88
/// ## What it does
9-
/// Checks for consistent style regarding whether tuples in subscripts
9+
/// Checks for consistent style regarding whether nonempty tuples in subscripts
1010
/// are parenthesized.
1111
///
1212
/// The exact nature of this violation depends on the setting
@@ -20,14 +20,14 @@ use crate::checkers::ast::Checker;
2020
/// ## Example
2121
///
2222
/// ```python
23-
/// directions = {(0, 1): "North", (-1, 0): "East", (0, -1): "South", (1, 0): "West"}
23+
/// directions = {(0, 1): "North", (1, 0): "East", (0, -1): "South", (-1, 0): "West"}
2424
/// directions[(0, 1)]
2525
/// ```
2626
///
2727
/// Use instead (with default setting):
2828
///
2929
/// ```python
30-
/// directions = {(0, 1): "North", (-1, 0): "East", (0, -1): "South", (1, 0): "West"}
30+
/// directions = {(0, 1): "North", (1, 0): "East", (0, -1): "South", (-1, 0): "West"}
3131
/// directions[0, 1]
3232
/// ```
3333
@@ -61,7 +61,7 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip
6161
let Some(tuple_subscript) = subscript.slice.as_tuple_expr() else {
6262
return;
6363
};
64-
if tuple_subscript.parenthesized == prefer_parentheses {
64+
if tuple_subscript.parenthesized == prefer_parentheses || tuple_subscript.elts.is_empty() {
6565
return;
6666
}
6767
let locator = checker.locator();

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF031_RUF031.py.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
155155
27 | d[1,]
156156
28 | d[(1,)]
157157
| ^^^^ RUF031
158+
29 | d[()] # empty tuples should be ignored
158159
|
159160
= help: Remove the parentheses.
160161

@@ -164,3 +165,4 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
164165
27 27 | d[1,]
165166
28 |-d[(1,)]
166167
28 |+d[1,]
168+
29 29 | d[()] # empty tuples should be ignored

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__prefer_parentheses_getitem_tuple.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ RUF031_prefer_parens.py:26:3: RUF031 [*] Use parentheses for tuples in subscript
117117
26 | d[1,]
118118
| ^^ RUF031
119119
27 | d[(1,)]
120+
28 | d[()] # empty tuples should be ignored
120121
|
121122
= help: Parenthesize the tuple.
122123

@@ -127,3 +128,4 @@ RUF031_prefer_parens.py:26:3: RUF031 [*] Use parentheses for tuples in subscript
127128
26 |-d[1,]
128129
27 26 | d[(1,)]
129130
27 |+d[(1,)]
131+
28 28 | d[()] # empty tuples should be ignored

0 commit comments

Comments
 (0)