Skip to content

Commit

Permalink
[ruff] Ignore empty tuples for `incorrectly-parenthesized-tuple-in-su…
Browse files Browse the repository at this point in the history
…bscript (RUF031)` (#12749)
  • Loading branch information
dylwil3 authored Aug 8, 2024
1 parent f537335 commit f577e03
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF031.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@

d[1,]
d[(1,)]
d[()] # empty tuples should be ignored
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
] = self._extract_raw_features_from_token
d[1,]
d[(1,)]
d[()] # empty tuples should be ignored
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;

/// ## What it does
/// Checks for consistent style regarding whether tuples in subscripts
/// Checks for consistent style regarding whether nonempty tuples in subscripts
/// are parenthesized.
///
/// The exact nature of this violation depends on the setting
Expand All @@ -20,14 +20,14 @@ use crate::checkers::ast::Checker;
/// ## Example
///
/// ```python
/// directions = {(0, 1): "North", (-1, 0): "East", (0, -1): "South", (1, 0): "West"}
/// directions = {(0, 1): "North", (1, 0): "East", (0, -1): "South", (-1, 0): "West"}
/// directions[(0, 1)]
/// ```
///
/// Use instead (with default setting):
///
/// ```python
/// directions = {(0, 1): "North", (-1, 0): "East", (0, -1): "South", (1, 0): "West"}
/// directions = {(0, 1): "North", (1, 0): "East", (0, -1): "South", (-1, 0): "West"}
/// directions[0, 1]
/// ```
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip
let Some(tuple_subscript) = subscript.slice.as_tuple_expr() else {
return;
};
if tuple_subscript.parenthesized == prefer_parentheses {
if tuple_subscript.parenthesized == prefer_parentheses || tuple_subscript.elts.is_empty() {
return;
}
let locator = checker.locator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
27 | d[1,]
28 | d[(1,)]
| ^^^^ RUF031
29 | d[()] # empty tuples should be ignored
|
= help: Remove the parentheses.

Expand All @@ -164,3 +165,4 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
27 27 | d[1,]
28 |-d[(1,)]
28 |+d[1,]
29 29 | d[()] # empty tuples should be ignored
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ RUF031_prefer_parens.py:26:3: RUF031 [*] Use parentheses for tuples in subscript
26 | d[1,]
| ^^ RUF031
27 | d[(1,)]
28 | d[()] # empty tuples should be ignored
|
= help: Parenthesize the tuple.

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

0 comments on commit f577e03

Please sign in to comment.