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

@ -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(),
}
}
}