-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ruff] Implement
incorrectly-parenthesized-tuple-in-subscript
(`RUF…
…031`) (#12480) Implements the new fixable lint rule `RUF031` which checks for the use or omission of parentheses around tuples in subscripts, depending on the setting `lint.ruff.parenthesize-tuple-in-getitem`. By default, the use of parentheses is considered a violation.
- Loading branch information
Showing
15 changed files
with
552 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"} | ||
d[(1,2)] | ||
d[( | ||
1, | ||
2 | ||
)] | ||
d[ | ||
1, | ||
2 | ||
] | ||
d[(2,4)] | ||
d[(5,6,7)] | ||
d[(8,)] | ||
d[tuple(1,2)] | ||
d[tuple(8)] | ||
d[1,2] | ||
d[3,4] | ||
d[5,6,7] | ||
e = {((1,2),(3,4)):"a"} | ||
e[((1,2),(3,4))] | ||
e[(1,2),(3,4)] | ||
|
||
token_features[ | ||
(window_position, feature_name) | ||
] = self._extract_raw_features_from_token | ||
|
||
d[1,] | ||
d[(1,)] |
27 changes: 27 additions & 0 deletions
27
crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"} | ||
d[(1,2)] | ||
d[( | ||
1, | ||
2 | ||
)] | ||
d[ | ||
1, | ||
2 | ||
] | ||
d[(2,4)] | ||
d[(5,6,7)] | ||
d[(8,)] | ||
d[tuple(1,2)] | ||
d[tuple(8)] | ||
d[1,2] | ||
d[3,4] | ||
d[5,6,7] | ||
e = {((1,2),(3,4)):"a"} | ||
e[((1,2),(3,4))] | ||
e[(1,2),(3,4)] | ||
|
||
token_features[ | ||
(window_position, feature_name) | ||
] = self._extract_raw_features_from_token | ||
d[1,] | ||
d[(1,)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::ExprSubscript; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for consistent style regarding whether tuples in subscripts | ||
/// are parenthesized. | ||
/// | ||
/// The exact nature of this violation depends on the setting | ||
/// [`lint.ruff.parenthesize-tuple-in-subscript`]. By default, the use of | ||
/// parentheses is considered a violation. | ||
/// | ||
/// ## Why is this bad? | ||
/// It is good to be consistent and, depending on the codebase, one or the other | ||
/// convention may be preferred. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// 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] | ||
/// ``` | ||
|
||
#[violation] | ||
pub struct IncorrectlyParenthesizedTupleInSubscript { | ||
prefer_parentheses: bool, | ||
} | ||
|
||
impl AlwaysFixableViolation for IncorrectlyParenthesizedTupleInSubscript { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
if self.prefer_parentheses { | ||
format!("Use parentheses for tuples in subscripts.") | ||
} else { | ||
format!("Avoid parentheses for tuples in subscripts.") | ||
} | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
if self.prefer_parentheses { | ||
"Parenthesize the tuple.".to_string() | ||
} else { | ||
"Remove the parentheses.".to_string() | ||
} | ||
} | ||
} | ||
|
||
/// RUF031 | ||
pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscript: &ExprSubscript) { | ||
let prefer_parentheses = checker.settings.ruff.parenthesize_tuple_in_subscript; | ||
let Some(tuple_subscript) = subscript.slice.as_tuple_expr() else { | ||
return; | ||
}; | ||
if tuple_subscript.parenthesized == prefer_parentheses { | ||
return; | ||
} | ||
let locator = checker.locator(); | ||
let source_range = subscript.slice.range(); | ||
let new_source = if prefer_parentheses { | ||
format!("({})", locator.slice(source_range)) | ||
} else { | ||
locator.slice(source_range)[1..source_range.len().to_usize() - 1].to_string() | ||
}; | ||
let edit = Edit::range_replacement(new_source, source_range); | ||
checker.diagnostics.push( | ||
Diagnostic::new( | ||
IncorrectlyParenthesizedTupleInSubscript { prefer_parentheses }, | ||
source_range, | ||
) | ||
.with_fix(Fix::safe_edit(edit)), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Settings for the `ruff` plugin. | ||
|
||
use crate::display_settings; | ||
use ruff_macros::CacheKey; | ||
use std::fmt; | ||
|
||
#[derive(Debug, Clone, CacheKey, Default)] | ||
pub struct Settings { | ||
pub parenthesize_tuple_in_subscript: bool, | ||
} | ||
|
||
impl fmt::Display for Settings { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
display_settings! { | ||
formatter = f, | ||
namespace = "linter.ruff", | ||
fields = [ | ||
self.parenthesize_tuple_in_subscript | ||
] | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.