[ruff] Implement incorrectly-parenthesized-tuple-in-subscript (RUF031) (#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.
This commit is contained in:
Dylan 2024-08-07 08:11:29 -05:00 committed by GitHub
parent d380b37a09
commit 7997da47f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 552 additions and 3 deletions

View file

@ -21,7 +21,7 @@ use ruff_linter::rules::{
flake8_copyright, flake8_errmsg, flake8_gettext, flake8_implicit_str_concat,
flake8_import_conventions, flake8_pytest_style, flake8_quotes, flake8_self,
flake8_tidy_imports, flake8_type_checking, flake8_unused_arguments, isort, mccabe, pep8_naming,
pycodestyle, pydocstyle, pyflakes, pylint, pyupgrade,
pycodestyle, pydocstyle, pyflakes, pylint, pyupgrade, ruff,
};
use ruff_linter::settings::types::{
IdentifierPattern, OutputFormat, PreviewMode, PythonVersion, RequiredVersion,
@ -455,6 +455,10 @@ pub struct LintOptions {
)]
pub exclude: Option<Vec<String>>,
/// Options for the `ruff` plugin
#[option_group]
pub ruff: Option<RuffOptions>,
/// Whether to enable preview mode. When preview mode is enabled, Ruff will
/// use unstable rules and fixes.
#[option(
@ -2969,6 +2973,35 @@ impl PyUpgradeOptions {
}
}
#[derive(
Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, OptionsMetadata, CombineOptions,
)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct RuffOptions {
/// Whether to prefer accessing items keyed by tuples with
/// parentheses around the tuple (see `RUF031`).
#[option(
default = r#"false"#,
value_type = "bool",
example = r#"
# Make it a violation to use a tuple in a subscript without parentheses.
parenthesize-tuple-in-subscript = true
"#
)]
pub parenthesize_tuple_in_subscript: Option<bool>,
}
impl RuffOptions {
pub fn into_settings(self) -> ruff::settings::Settings {
ruff::settings::Settings {
parenthesize_tuple_in_subscript: self
.parenthesize_tuple_in_subscript
.unwrap_or_default(),
}
}
}
/// Configures the way Ruff formats your code.
#[derive(
Clone, Debug, PartialEq, Eq, Default, Deserialize, Serialize, OptionsMetadata, CombineOptions,