Add allow-unused-imports setting for unused-import rule (F401) (#13601)

## Summary
Resolves https://github.com/astral-sh/ruff/issues/9962 by allowing a
configuration setting `allowed-unused-imports`

TODO:
- [x] Figure out the correct name and place for the setting; currently,
I have added it top level.
- [x] The comparison is pretty naive. I tried using `glob::Pattern` but
couldn't get it to work in the configuration.
- [x] Add tests
- [x] Update documentations

## Test Plan

`cargo test`
This commit is contained in:
Simon Høxbro Hansen 2024-10-03 21:44:44 +02:00 committed by GitHub
parent 4aefe52393
commit 7ad07c2c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 132 additions and 4 deletions

View file

@ -626,6 +626,7 @@ pub struct LintConfiguration {
pub logger_objects: Option<Vec<String>>,
pub task_tags: Option<Vec<String>>,
pub typing_modules: Option<Vec<String>>,
pub allowed_unused_imports: Option<Vec<String>>,
// Plugins
pub flake8_annotations: Option<Flake8AnnotationsOptions>,
@ -738,6 +739,7 @@ impl LintConfiguration {
task_tags: options.common.task_tags,
logger_objects: options.common.logger_objects,
typing_modules: options.common.typing_modules,
allowed_unused_imports: options.common.allowed_unused_imports,
// Plugins
flake8_annotations: options.common.flake8_annotations,
flake8_bandit: options.common.flake8_bandit,
@ -1106,6 +1108,9 @@ impl LintConfiguration {
.or(config.explicit_preview_rules),
task_tags: self.task_tags.or(config.task_tags),
typing_modules: self.typing_modules.or(config.typing_modules),
allowed_unused_imports: self
.allowed_unused_imports
.or(config.allowed_unused_imports),
// Plugins
flake8_annotations: self.flake8_annotations.combine(config.flake8_annotations),
flake8_bandit: self.flake8_bandit.combine(config.flake8_bandit),
@ -1327,6 +1332,7 @@ fn warn_about_deprecated_top_level_lint_options(
explicit_preview_rules,
task_tags,
typing_modules,
allowed_unused_imports,
unfixable,
flake8_annotations,
flake8_bandit,
@ -1425,6 +1431,9 @@ fn warn_about_deprecated_top_level_lint_options(
if typing_modules.is_some() {
used_options.push("typing-modules");
}
if allowed_unused_imports.is_some() {
used_options.push("allowed-unused-imports");
}
if unfixable.is_some() {
used_options.push("unfixable");

View file

@ -796,6 +796,16 @@ pub struct LintCommonOptions {
)]
pub typing_modules: Option<Vec<String>>,
/// A list of modules which is allowed even thought they are not used
/// in the code.
///
/// This is useful when a module has a side effect when imported.
#[option(
default = r#"[]"#,
value_type = "list[str]",
example = r#"allowed-unused-imports = ["hvplot.pandas"]"#
)]
pub allowed_unused_imports: Option<Vec<String>>,
/// A list of rule codes or prefixes to consider non-fixable.
#[option(
default = "[]",
@ -2812,12 +2822,28 @@ pub struct PyflakesOptions {
example = "extend-generics = [\"django.db.models.ForeignKey\"]"
)]
pub extend_generics: Option<Vec<String>>,
/// A list of modules to ignore when considering unused imports.
///
/// Used to prevent violations for specific modules that are known to have side effects on
/// import (e.g., `hvplot.pandas`).
///
/// Modules in this list are expected to be fully-qualified names (e.g., `hvplot.pandas`). Any
/// submodule of a given module will also be ignored (e.g., given `hvplot`, `hvplot.pandas`
/// will also be ignored).
#[option(
default = r#"[]"#,
value_type = "list[str]",
example = r#"allowed-unused-imports = ["hvplot.pandas"]"#
)]
pub allowed_unused_imports: Option<Vec<String>>,
}
impl PyflakesOptions {
pub fn into_settings(self) -> pyflakes::settings::Settings {
pyflakes::settings::Settings {
extend_generics: self.extend_generics.unwrap_or_default(),
allowed_unused_imports: self.allowed_unused_imports.unwrap_or_default(),
}
}
}