mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
[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:
parent
d380b37a09
commit
7997da47f5
15 changed files with 552 additions and 3 deletions
|
@ -47,7 +47,7 @@ use crate::options::{
|
|||
Flake8SelfOptions, Flake8TidyImportsOptions, Flake8TypeCheckingOptions,
|
||||
Flake8UnusedArgumentsOptions, FormatOptions, IsortOptions, LintCommonOptions, LintOptions,
|
||||
McCabeOptions, Options, Pep8NamingOptions, PyUpgradeOptions, PycodestyleOptions,
|
||||
PydocstyleOptions, PyflakesOptions, PylintOptions,
|
||||
PydocstyleOptions, PyflakesOptions, PylintOptions, RuffOptions,
|
||||
};
|
||||
use crate::settings::{
|
||||
FileResolverSettings, FormatterSettings, LineEnding, Settings, EXCLUDE, INCLUDE,
|
||||
|
@ -402,6 +402,10 @@ impl Configuration {
|
|||
.pyupgrade
|
||||
.map(PyUpgradeOptions::into_settings)
|
||||
.unwrap_or_default(),
|
||||
ruff: lint
|
||||
.ruff
|
||||
.map(RuffOptions::into_settings)
|
||||
.unwrap_or_default(),
|
||||
},
|
||||
|
||||
formatter,
|
||||
|
@ -631,6 +635,7 @@ pub struct LintConfiguration {
|
|||
pub pyflakes: Option<PyflakesOptions>,
|
||||
pub pylint: Option<PylintOptions>,
|
||||
pub pyupgrade: Option<PyUpgradeOptions>,
|
||||
pub ruff: Option<RuffOptions>,
|
||||
}
|
||||
|
||||
impl LintConfiguration {
|
||||
|
@ -741,6 +746,7 @@ impl LintConfiguration {
|
|||
pyflakes: options.common.pyflakes,
|
||||
pylint: options.common.pylint,
|
||||
pyupgrade: options.common.pyupgrade,
|
||||
ruff: options.ruff,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1118,6 +1124,7 @@ impl LintConfiguration {
|
|||
pyflakes: self.pyflakes.combine(config.pyflakes),
|
||||
pylint: self.pylint.combine(config.pylint),
|
||||
pyupgrade: self.pyupgrade.combine(config.pyupgrade),
|
||||
ruff: self.ruff.combine(config.ruff),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue