Implement flake8-tidy-imports (#789)

This commit is contained in:
Charlie Marsh 2022-11-17 11:44:06 -05:00 committed by GitHub
parent c59e1ff0b5
commit f0a54716e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 373 additions and 9 deletions

25
LICENSE
View file

@ -218,6 +218,31 @@ are:
SOFTWARE. SOFTWARE.
""" """
- flake8-tidy-imports, licensed as follows:
"""
MIT License
Copyright (c) 2017 Adam Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
- flake8-print, licensed as follows: - flake8-print, licensed as follows:
""" """
MIT License MIT License

View file

@ -548,6 +548,14 @@ For more, see [flake8-builtins](https://pypi.org/project/flake8-builtins/2.0.1/)
| A002 | BuiltinArgumentShadowing | Argument `...` is shadowing a python builtin | | | A002 | BuiltinArgumentShadowing | Argument `...` is shadowing a python builtin | |
| A003 | BuiltinAttributeShadowing | Class attribute `...` is shadowing a python builtin | | | A003 | BuiltinAttributeShadowing | Class attribute `...` is shadowing a python builtin | |
### flake8-tidy-imports
For more, see [flake8-tidy-imports](https://pypi.org/project/flake8-tidy-imports/4.8.0/) on PyPI.
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| I252 | BannedRelativeImport | Relative imports are banned | |
### flake8-print ### flake8-print
For more, see [flake8-print](https://pypi.org/project/flake8-print/5.0.0/) on PyPI. For more, see [flake8-print](https://pypi.org/project/flake8-print/5.0.0/) on PyPI.

View file

@ -3,9 +3,10 @@ use std::collections::{BTreeSet, HashMap};
use anyhow::Result; use anyhow::Result;
use ruff::checks_gen::CheckCodePrefix; use ruff::checks_gen::CheckCodePrefix;
use ruff::flake8_quotes::settings::Quote; use ruff::flake8_quotes::settings::Quote;
use ruff::flake8_tidy_imports::settings::Strictness;
use ruff::settings::options::Options; use ruff::settings::options::Options;
use ruff::settings::pyproject::Pyproject; use ruff::settings::pyproject::Pyproject;
use ruff::{flake8_annotations, flake8_bugbear, flake8_quotes, pep8_naming}; use ruff::{flake8_annotations, flake8_bugbear, flake8_quotes, flake8_tidy_imports, pep8_naming};
use crate::plugin::Plugin; use crate::plugin::Plugin;
use crate::{parser, plugin}; use crate::{parser, plugin};
@ -71,6 +72,7 @@ pub fn convert(
let mut flake8_annotations: flake8_annotations::settings::Options = Default::default(); let mut flake8_annotations: flake8_annotations::settings::Options = Default::default();
let mut flake8_bugbear: flake8_bugbear::settings::Options = Default::default(); let mut flake8_bugbear: flake8_bugbear::settings::Options = Default::default();
let mut flake8_quotes: flake8_quotes::settings::Options = Default::default(); let mut flake8_quotes: flake8_quotes::settings::Options = Default::default();
let mut flake8_tidy_imports: flake8_tidy_imports::settings::Options = Default::default();
let mut pep8_naming: pep8_naming::settings::Options = Default::default(); let mut pep8_naming: pep8_naming::settings::Options = Default::default();
for (key, value) in flake8 { for (key, value) in flake8 {
if let Some(value) = value { if let Some(value) = value {
@ -172,6 +174,14 @@ pub fn convert(
pep8_naming.staticmethod_decorators = pep8_naming.staticmethod_decorators =
Some(parser::parse_strings(value.as_ref())); Some(parser::parse_strings(value.as_ref()));
} }
// flake8-tidy-imports
"ban-relative-imports" | "ban_relative_imports" => match value.trim() {
"true" => flake8_tidy_imports.ban_relative_imports = Some(Strictness::All),
"parents" => {
flake8_tidy_imports.ban_relative_imports = Some(Strictness::Parents)
}
_ => eprintln!("Unexpected '{key}' value: {value}"),
},
// flake8-docstrings // flake8-docstrings
"docstring-convention" => { "docstring-convention" => {
// No-op (handled above). // No-op (handled above).
@ -194,6 +204,9 @@ pub fn convert(
if flake8_quotes != Default::default() { if flake8_quotes != Default::default() {
options.flake8_quotes = Some(flake8_quotes); options.flake8_quotes = Some(flake8_quotes);
} }
if flake8_tidy_imports != Default::default() {
options.flake8_tidy_imports = Some(flake8_tidy_imports);
}
if pep8_naming != Default::default() { if pep8_naming != Default::default() {
options.pep8_naming = Some(pep8_naming); options.pep8_naming = Some(pep8_naming);
} }
@ -238,6 +251,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -272,6 +286,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -306,6 +321,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -340,6 +356,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -379,6 +396,7 @@ mod tests {
docstring_quotes: None, docstring_quotes: None,
avoid_escape: None, avoid_escape: None,
}), }),
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -451,6 +469,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });
@ -491,6 +510,7 @@ mod tests {
docstring_quotes: None, docstring_quotes: None,
avoid_escape: None, avoid_escape: None,
}), }),
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}); });

View file

@ -11,6 +11,7 @@ pub enum Plugin {
Flake8Builtins, Flake8Builtins,
Flake8Comprehensions, Flake8Comprehensions,
Flake8Docstrings, Flake8Docstrings,
Flake8TidyImports,
Flake8Print, Flake8Print,
Flake8Quotes, Flake8Quotes,
Flake8Annotations, Flake8Annotations,
@ -28,6 +29,7 @@ impl FromStr for Plugin {
"flake8-builtins" => Ok(Plugin::Flake8Builtins), "flake8-builtins" => Ok(Plugin::Flake8Builtins),
"flake8-comprehensions" => Ok(Plugin::Flake8Comprehensions), "flake8-comprehensions" => Ok(Plugin::Flake8Comprehensions),
"flake8-docstrings" => Ok(Plugin::Flake8Docstrings), "flake8-docstrings" => Ok(Plugin::Flake8Docstrings),
"flake8-tidy-imports" => Ok(Plugin::Flake8TidyImports),
"flake8-print" => Ok(Plugin::Flake8Print), "flake8-print" => Ok(Plugin::Flake8Print),
"flake8-quotes" => Ok(Plugin::Flake8Quotes), "flake8-quotes" => Ok(Plugin::Flake8Quotes),
"flake8-annotations" => Ok(Plugin::Flake8Annotations), "flake8-annotations" => Ok(Plugin::Flake8Annotations),
@ -46,6 +48,7 @@ impl Plugin {
Plugin::Flake8Builtins => CheckCodePrefix::A, Plugin::Flake8Builtins => CheckCodePrefix::A,
Plugin::Flake8Comprehensions => CheckCodePrefix::C, Plugin::Flake8Comprehensions => CheckCodePrefix::C,
Plugin::Flake8Docstrings => CheckCodePrefix::D, Plugin::Flake8Docstrings => CheckCodePrefix::D,
Plugin::Flake8TidyImports => CheckCodePrefix::I25,
Plugin::Flake8Print => CheckCodePrefix::T, Plugin::Flake8Print => CheckCodePrefix::T,
Plugin::Flake8Quotes => CheckCodePrefix::Q, Plugin::Flake8Quotes => CheckCodePrefix::Q,
Plugin::Flake8Annotations => CheckCodePrefix::ANN, Plugin::Flake8Annotations => CheckCodePrefix::ANN,
@ -76,6 +79,7 @@ impl Plugin {
// Default to PEP8. // Default to PEP8.
DocstringConvention::PEP8.select() DocstringConvention::PEP8.select()
} }
Plugin::Flake8TidyImports => vec![CheckCodePrefix::I25],
Plugin::Flake8Print => vec![CheckCodePrefix::T], Plugin::Flake8Print => vec![CheckCodePrefix::T],
Plugin::Flake8Quotes => vec![CheckCodePrefix::Q], Plugin::Flake8Quotes => vec![CheckCodePrefix::Q],
Plugin::Flake8Annotations => vec![CheckCodePrefix::ANN], Plugin::Flake8Annotations => vec![CheckCodePrefix::ANN],
@ -315,6 +319,13 @@ pub fn infer_plugins_from_options(flake8: &HashMap<String, Option<String>>) -> V
"allow-star-arg-any" | "allow_star_arg_any" => { "allow-star-arg-any" | "allow_star_arg_any" => {
plugins.insert(Plugin::Flake8Annotations); plugins.insert(Plugin::Flake8Annotations);
} }
// flake8-tidy-imports
"ban-relative-imports" | "ban_relative_imports" => {
plugins.insert(Plugin::Flake8TidyImports);
}
"banned-modules" | "banned_modules" => {
plugins.insert(Plugin::Flake8TidyImports);
}
// pep8-naming // pep8-naming
"ignore-names" | "ignore_names" => { "ignore-names" | "ignore_names" => {
plugins.insert(Plugin::PEP8Naming); plugins.insert(Plugin::PEP8Naming);
@ -342,6 +353,7 @@ pub fn infer_plugins_from_codes(codes: &BTreeSet<CheckCodePrefix>) -> Vec<Plugin
Plugin::Flake8Builtins, Plugin::Flake8Builtins,
Plugin::Flake8Comprehensions, Plugin::Flake8Comprehensions,
Plugin::Flake8Docstrings, Plugin::Flake8Docstrings,
Plugin::Flake8TidyImports,
Plugin::Flake8Print, Plugin::Flake8Print,
Plugin::Flake8Quotes, Plugin::Flake8Quotes,
Plugin::Flake8Annotations, Plugin::Flake8Annotations,

12
resources/test/fixtures/I252.py vendored Normal file
View file

@ -0,0 +1,12 @@
from . import sibling
from .sibling import example
from .. import parent
from ..parent import example
from ... import grandparent
from ...grandparent import example
import other
import other.example
from other import example

View file

@ -38,3 +38,6 @@ classmethod-decorators = [
staticmethod-decorators = [ staticmethod-decorators = [
"staticmethod", "staticmethod",
] ]
[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "parents"

View file

@ -37,7 +37,8 @@ use crate::source_code_locator::SourceCodeLocator;
use crate::visibility::{module_visibility, transition_scope, Modifier, Visibility, VisibleScope}; use crate::visibility::{module_visibility, transition_scope, Modifier, Visibility, VisibleScope};
use crate::{ use crate::{
docstrings, flake8_2020, flake8_annotations, flake8_bandit, flake8_bugbear, flake8_builtins, docstrings, flake8_2020, flake8_annotations, flake8_bandit, flake8_bugbear, flake8_builtins,
flake8_comprehensions, flake8_print, pep8_naming, pycodestyle, pydocstyle, pyflakes, pyupgrade, flake8_comprehensions, flake8_print, flake8_tidy_imports, pep8_naming, pycodestyle, pydocstyle,
pyflakes, pyupgrade,
}; };
const GLOBAL_SCOPE_INDEX: usize = 0; const GLOBAL_SCOPE_INDEX: usize = 0;
@ -782,6 +783,16 @@ where
) )
} }
if self.settings.enabled.contains(&CheckCode::I252) {
if let Some(check) = flake8_tidy_imports::checks::banned_relative_import(
stmt,
level.as_ref(),
&self.settings.flake8_tidy_imports.ban_relative_imports,
) {
self.add_check(check);
}
}
if let Some(asname) = &alias.node.asname { if let Some(asname) = &alias.node.asname {
if self.settings.enabled.contains(&CheckCode::N811) { if self.settings.enabled.contains(&CheckCode::N811) {
if let Some(check) = if let Some(check) =

View file

@ -8,6 +8,7 @@ use strum_macros::{AsRefStr, EnumIter, EnumString};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::autofix::Fix; use crate::autofix::Fix;
use crate::flake8_quotes::settings::Quote; use crate::flake8_quotes::settings::Quote;
use crate::flake8_tidy_imports::settings::Strictness;
use crate::pyupgrade::types::Primitive; use crate::pyupgrade::types::Primitive;
#[derive( #[derive(
@ -119,6 +120,8 @@ pub enum CheckCode {
C415, C415,
C416, C416,
C417, C417,
// flake8-tidy-imports
I252,
// flake8-print // flake8-print
T201, T201,
T203, T203,
@ -252,6 +255,7 @@ pub enum CheckCategory {
Flake8Comprehensions, Flake8Comprehensions,
Flake8Bugbear, Flake8Bugbear,
Flake8Builtins, Flake8Builtins,
Flake8TidyImports,
Flake8Print, Flake8Print,
Flake8Quotes, Flake8Quotes,
Flake8Annotations, Flake8Annotations,
@ -270,6 +274,7 @@ impl CheckCategory {
CheckCategory::Flake8Builtins => "flake8-builtins", CheckCategory::Flake8Builtins => "flake8-builtins",
CheckCategory::Flake8Bugbear => "flake8-bugbear", CheckCategory::Flake8Bugbear => "flake8-bugbear",
CheckCategory::Flake8Comprehensions => "flake8-comprehensions", CheckCategory::Flake8Comprehensions => "flake8-comprehensions",
CheckCategory::Flake8TidyImports => "flake8-tidy-imports",
CheckCategory::Flake8Print => "flake8-print", CheckCategory::Flake8Print => "flake8-print",
CheckCategory::Flake8Quotes => "flake8-quotes", CheckCategory::Flake8Quotes => "flake8-quotes",
CheckCategory::Flake8Annotations => "flake8-annotations", CheckCategory::Flake8Annotations => "flake8-annotations",
@ -296,6 +301,9 @@ impl CheckCategory {
CheckCategory::Flake8Comprehensions => { CheckCategory::Flake8Comprehensions => {
Some("https://pypi.org/project/flake8-comprehensions/3.10.1/") Some("https://pypi.org/project/flake8-comprehensions/3.10.1/")
} }
CheckCategory::Flake8TidyImports => {
Some("https://pypi.org/project/flake8-tidy-imports/4.8.0/")
}
CheckCategory::Flake8Print => Some("https://pypi.org/project/flake8-print/5.0.0/"), CheckCategory::Flake8Print => Some("https://pypi.org/project/flake8-print/5.0.0/"),
CheckCategory::Flake8Quotes => Some("https://pypi.org/project/flake8-quotes/3.3.1/"), CheckCategory::Flake8Quotes => Some("https://pypi.org/project/flake8-quotes/3.3.1/"),
CheckCategory::Flake8Annotations => { CheckCategory::Flake8Annotations => {
@ -423,6 +431,8 @@ pub enum CheckKind {
UnnecessarySubscriptReversal(String), UnnecessarySubscriptReversal(String),
UnnecessaryComprehension(String), UnnecessaryComprehension(String),
UnnecessaryMap(String), UnnecessaryMap(String),
// flake8-tidy-imports
BannedRelativeImport(Strictness),
// flake8-print // flake8-print
PrintFound, PrintFound,
PPrintFound, PPrintFound,
@ -682,6 +692,8 @@ impl CheckCode {
} }
CheckCode::C416 => CheckKind::UnnecessaryComprehension("(list|set)".to_string()), CheckCode::C416 => CheckKind::UnnecessaryComprehension("(list|set)".to_string()),
CheckCode::C417 => CheckKind::UnnecessaryMap("(list|set|dict)".to_string()), CheckCode::C417 => CheckKind::UnnecessaryMap("(list|set|dict)".to_string()),
// flake8-tidy-imports
CheckCode::I252 => CheckKind::BannedRelativeImport(Strictness::All),
// flake8-print // flake8-print
CheckCode::T201 => CheckKind::PrintFound, CheckCode::T201 => CheckKind::PrintFound,
CheckCode::T203 => CheckKind::PPrintFound, CheckCode::T203 => CheckKind::PPrintFound,
@ -913,6 +925,7 @@ impl CheckCode {
CheckCode::C415 => CheckCategory::Flake8Comprehensions, CheckCode::C415 => CheckCategory::Flake8Comprehensions,
CheckCode::C416 => CheckCategory::Flake8Comprehensions, CheckCode::C416 => CheckCategory::Flake8Comprehensions,
CheckCode::C417 => CheckCategory::Flake8Comprehensions, CheckCode::C417 => CheckCategory::Flake8Comprehensions,
CheckCode::I252 => CheckCategory::Flake8TidyImports,
CheckCode::T201 => CheckCategory::Flake8Print, CheckCode::T201 => CheckCategory::Flake8Print,
CheckCode::T203 => CheckCategory::Flake8Print, CheckCode::T203 => CheckCategory::Flake8Print,
CheckCode::Q000 => CheckCategory::Flake8Quotes, CheckCode::Q000 => CheckCategory::Flake8Quotes,
@ -1122,6 +1135,8 @@ impl CheckKind {
CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415, CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415,
CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416, CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416,
CheckKind::UnnecessaryMap(_) => &CheckCode::C417, CheckKind::UnnecessaryMap(_) => &CheckCode::C417,
// flake8-tidy-imports
CheckKind::BannedRelativeImport(_) => &CheckCode::I252,
// flake8-print // flake8-print
CheckKind::PrintFound => &CheckCode::T201, CheckKind::PrintFound => &CheckCode::T201,
CheckKind::PPrintFound => &CheckCode::T203, CheckKind::PPrintFound => &CheckCode::T203,
@ -1574,6 +1589,13 @@ impl CheckKind {
format!("Unnecessary `map` usage (rewrite using a `{obj_type}` comprehension)") format!("Unnecessary `map` usage (rewrite using a `{obj_type}` comprehension)")
} }
} }
// flake8-tidy-imports
CheckKind::BannedRelativeImport(strictness) => match strictness {
Strictness::Parents => {
"Relative imports from parent modules are banned".to_string()
}
Strictness::All => "Relative imports are banned".to_string(),
},
// flake8-print // flake8-print
CheckKind::PrintFound => "`print` found".to_string(), CheckKind::PrintFound => "`print` found".to_string(),
CheckKind::PPrintFound => "`pprint` found".to_string(), CheckKind::PPrintFound => "`pprint` found".to_string(),

View file

@ -215,6 +215,9 @@ pub enum CheckCodePrefix {
I0, I0,
I00, I00,
I001, I001,
I2,
I25,
I252,
M, M,
M0, M0,
M00, M00,
@ -925,10 +928,13 @@ impl CheckCodePrefix {
CheckCodePrefix::F9 => vec![CheckCode::F901], CheckCodePrefix::F9 => vec![CheckCode::F901],
CheckCodePrefix::F90 => vec![CheckCode::F901], CheckCodePrefix::F90 => vec![CheckCode::F901],
CheckCodePrefix::F901 => vec![CheckCode::F901], CheckCodePrefix::F901 => vec![CheckCode::F901],
CheckCodePrefix::I => vec![CheckCode::I001], CheckCodePrefix::I => vec![CheckCode::I252, CheckCode::I001],
CheckCodePrefix::I0 => vec![CheckCode::I001], CheckCodePrefix::I0 => vec![CheckCode::I001],
CheckCodePrefix::I00 => vec![CheckCode::I001], CheckCodePrefix::I00 => vec![CheckCode::I001],
CheckCodePrefix::I001 => vec![CheckCode::I001], CheckCodePrefix::I001 => vec![CheckCode::I001],
CheckCodePrefix::I2 => vec![CheckCode::I252],
CheckCodePrefix::I25 => vec![CheckCode::I252],
CheckCodePrefix::I252 => vec![CheckCode::I252],
CheckCodePrefix::M => vec![CheckCode::M001], CheckCodePrefix::M => vec![CheckCode::M001],
CheckCodePrefix::M0 => vec![CheckCode::M001], CheckCodePrefix::M0 => vec![CheckCode::M001],
CheckCodePrefix::M00 => vec![CheckCode::M001], CheckCodePrefix::M00 => vec![CheckCode::M001],
@ -1372,6 +1378,9 @@ impl CheckCodePrefix {
CheckCodePrefix::I0 => PrefixSpecificity::Hundreds, CheckCodePrefix::I0 => PrefixSpecificity::Hundreds,
CheckCodePrefix::I00 => PrefixSpecificity::Tens, CheckCodePrefix::I00 => PrefixSpecificity::Tens,
CheckCodePrefix::I001 => PrefixSpecificity::Explicit, CheckCodePrefix::I001 => PrefixSpecificity::Explicit,
CheckCodePrefix::I2 => PrefixSpecificity::Hundreds,
CheckCodePrefix::I25 => PrefixSpecificity::Tens,
CheckCodePrefix::I252 => PrefixSpecificity::Explicit,
CheckCodePrefix::M => PrefixSpecificity::Category, CheckCodePrefix::M => PrefixSpecificity::Category,
CheckCodePrefix::M0 => PrefixSpecificity::Hundreds, CheckCodePrefix::M0 => PrefixSpecificity::Hundreds,
CheckCodePrefix::M00 => PrefixSpecificity::Tens, CheckCodePrefix::M00 => PrefixSpecificity::Tens,

View file

@ -1,4 +1,4 @@
//! Settings for the `flake-quotes` plugin. //! Settings for the `flake8-quotes` plugin.
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View file

@ -0,0 +1,26 @@
use rustpython_ast::Stmt;
use crate::ast::types::Range;
use crate::checks::{Check, CheckKind};
use crate::flake8_tidy_imports::settings::Strictness;
pub fn banned_relative_import(
stmt: &Stmt,
level: Option<&usize>,
strictness: &Strictness,
) -> Option<Check> {
if let Some(level) = level {
if level
> &match strictness {
Strictness::All => 0,
Strictness::Parents => 1,
}
{
return Some(Check::new(
CheckKind::BannedRelativeImport(strictness.clone()),
Range::from_located(stmt),
));
}
}
None
}

View file

@ -0,0 +1,49 @@
pub mod checks;
pub mod settings;
#[cfg(test)]
mod tests {
use std::path::Path;
use anyhow::Result;
use crate::autofix::fixer;
use crate::checks::CheckCode;
use crate::flake8_tidy_imports::settings::Strictness;
use crate::linter::test_path;
use crate::{flake8_tidy_imports, Settings};
#[test]
fn ban_parent_imports() -> Result<()> {
let mut checks = test_path(
Path::new("./resources/test/fixtures/I252.py"),
&Settings {
flake8_tidy_imports: flake8_tidy_imports::settings::Settings {
ban_relative_imports: Strictness::Parents,
},
..Settings::for_rules(vec![CheckCode::I252])
},
&fixer::Mode::Generate,
)?;
checks.sort_by_key(|check| check.location);
insta::assert_yaml_snapshot!(checks);
Ok(())
}
#[test]
fn ban_all_imports() -> Result<()> {
let mut checks = test_path(
Path::new("./resources/test/fixtures/I252.py"),
&Settings {
flake8_tidy_imports: flake8_tidy_imports::settings::Settings {
ban_relative_imports: Strictness::All,
},
..Settings::for_rules(vec![CheckCode::I252])
},
&fixer::Mode::Generate,
)?;
checks.sort_by_key(|check| check.location);
insta::assert_yaml_snapshot!(checks);
Ok(())
}
}

View file

@ -0,0 +1,37 @@
//! Settings for the `flake8-tidy-imports` plugin.
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum Strictness {
Parents,
All,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Options {
pub ban_relative_imports: Option<Strictness>,
}
#[derive(Debug, Hash)]
pub struct Settings {
pub ban_relative_imports: Strictness,
}
impl Settings {
pub fn from_options(options: Options) -> Self {
Self {
ban_relative_imports: options.ban_relative_imports.unwrap_or(Strictness::Parents),
}
}
}
impl Default for Settings {
fn default() -> Self {
Self {
ban_relative_imports: Strictness::Parents,
}
}
}

View file

@ -0,0 +1,59 @@
---
source: src/flake8_tidy_imports/mod.rs
expression: checks
---
- kind:
BannedRelativeImport: all
location:
row: 1
column: 0
end_location:
row: 1
column: 21
fix: ~
- kind:
BannedRelativeImport: all
location:
row: 2
column: 0
end_location:
row: 2
column: 28
fix: ~
- kind:
BannedRelativeImport: all
location:
row: 4
column: 0
end_location:
row: 4
column: 21
fix: ~
- kind:
BannedRelativeImport: all
location:
row: 5
column: 0
end_location:
row: 5
column: 28
fix: ~
- kind:
BannedRelativeImport: all
location:
row: 7
column: 0
end_location:
row: 7
column: 27
fix: ~
- kind:
BannedRelativeImport: all
location:
row: 8
column: 0
end_location:
row: 8
column: 34
fix: ~

View file

@ -0,0 +1,41 @@
---
source: src/flake8_tidy_imports/mod.rs
expression: checks
---
- kind:
BannedRelativeImport: parents
location:
row: 4
column: 0
end_location:
row: 4
column: 21
fix: ~
- kind:
BannedRelativeImport: parents
location:
row: 5
column: 0
end_location:
row: 5
column: 28
fix: ~
- kind:
BannedRelativeImport: parents
location:
row: 7
column: 0
end_location:
row: 7
column: 27
fix: ~
- kind:
BannedRelativeImport: parents
location:
row: 8
column: 0
end_location:
row: 8
column: 34
fix: ~

View file

@ -35,6 +35,7 @@ mod flake8_builtins;
mod flake8_comprehensions; mod flake8_comprehensions;
mod flake8_print; mod flake8_print;
pub mod flake8_quotes; pub mod flake8_quotes;
pub mod flake8_tidy_imports;
pub mod fs; pub mod fs;
mod isort; mod isort;
mod lex; mod lex;

View file

@ -12,7 +12,9 @@ use regex::Regex;
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
use crate::settings::pyproject::load_options; use crate::settings::pyproject::load_options;
use crate::settings::types::{FilePattern, PerFileIgnore, PythonVersion}; use crate::settings::types::{FilePattern, PerFileIgnore, PythonVersion};
use crate::{flake8_annotations, flake8_bugbear, flake8_quotes, fs, isort, pep8_naming}; use crate::{
flake8_annotations, flake8_bugbear, flake8_quotes, flake8_tidy_imports, fs, isort, pep8_naming,
};
#[derive(Debug)] #[derive(Debug)]
pub struct Configuration { pub struct Configuration {
@ -32,6 +34,7 @@ pub struct Configuration {
pub flake8_annotations: flake8_annotations::settings::Settings, pub flake8_annotations: flake8_annotations::settings::Settings,
pub flake8_bugbear: flake8_bugbear::settings::Settings, pub flake8_bugbear: flake8_bugbear::settings::Settings,
pub flake8_quotes: flake8_quotes::settings::Settings, pub flake8_quotes: flake8_quotes::settings::Settings,
pub flake8_tidy_imports: flake8_tidy_imports::settings::Settings,
pub isort: isort::settings::Settings, pub isort: isort::settings::Settings,
pub pep8_naming: pep8_naming::settings::Settings, pub pep8_naming: pep8_naming::settings::Settings,
} }
@ -142,6 +145,10 @@ impl Configuration {
.flake8_quotes .flake8_quotes
.map(flake8_quotes::settings::Settings::from_options) .map(flake8_quotes::settings::Settings::from_options)
.unwrap_or_default(), .unwrap_or_default(),
flake8_tidy_imports: options
.flake8_tidy_imports
.map(flake8_tidy_imports::settings::Settings::from_options)
.unwrap_or_default(),
isort: options isort: options
.isort .isort
.map(isort::settings::Settings::from_options) .map(isort::settings::Settings::from_options)

View file

@ -13,7 +13,9 @@ use crate::checks::CheckCode;
use crate::checks_gen::{CheckCodePrefix, PrefixSpecificity}; use crate::checks_gen::{CheckCodePrefix, PrefixSpecificity};
use crate::settings::configuration::Configuration; use crate::settings::configuration::Configuration;
use crate::settings::types::{FilePattern, PerFileIgnore, PythonVersion}; use crate::settings::types::{FilePattern, PerFileIgnore, PythonVersion};
use crate::{flake8_annotations, flake8_bugbear, flake8_quotes, isort, pep8_naming}; use crate::{
flake8_annotations, flake8_bugbear, flake8_quotes, flake8_tidy_imports, isort, pep8_naming,
};
pub mod configuration; pub mod configuration;
pub mod options; pub mod options;
@ -35,6 +37,7 @@ pub struct Settings {
pub flake8_annotations: flake8_annotations::settings::Settings, pub flake8_annotations: flake8_annotations::settings::Settings,
pub flake8_bugbear: flake8_bugbear::settings::Settings, pub flake8_bugbear: flake8_bugbear::settings::Settings,
pub flake8_quotes: flake8_quotes::settings::Settings, pub flake8_quotes: flake8_quotes::settings::Settings,
pub flake8_tidy_imports: flake8_tidy_imports::settings::Settings,
pub isort: isort::settings::Settings, pub isort: isort::settings::Settings,
pub pep8_naming: pep8_naming::settings::Settings, pub pep8_naming: pep8_naming::settings::Settings,
} }
@ -54,6 +57,7 @@ impl Settings {
flake8_annotations: config.flake8_annotations, flake8_annotations: config.flake8_annotations,
flake8_bugbear: config.flake8_bugbear, flake8_bugbear: config.flake8_bugbear,
flake8_quotes: config.flake8_quotes, flake8_quotes: config.flake8_quotes,
flake8_tidy_imports: config.flake8_tidy_imports,
isort: config.isort, isort: config.isort,
line_length: config.line_length, line_length: config.line_length,
pep8_naming: config.pep8_naming, pep8_naming: config.pep8_naming,
@ -76,6 +80,7 @@ impl Settings {
flake8_annotations: Default::default(), flake8_annotations: Default::default(),
flake8_bugbear: Default::default(), flake8_bugbear: Default::default(),
flake8_quotes: Default::default(), flake8_quotes: Default::default(),
flake8_tidy_imports: Default::default(),
isort: Default::default(), isort: Default::default(),
pep8_naming: Default::default(), pep8_naming: Default::default(),
} }
@ -94,6 +99,7 @@ impl Settings {
flake8_annotations: Default::default(), flake8_annotations: Default::default(),
flake8_bugbear: Default::default(), flake8_bugbear: Default::default(),
flake8_quotes: Default::default(), flake8_quotes: Default::default(),
flake8_tidy_imports: Default::default(),
isort: Default::default(), isort: Default::default(),
pep8_naming: Default::default(), pep8_naming: Default::default(),
} }

View file

@ -5,7 +5,9 @@ use serde::{Deserialize, Serialize};
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
use crate::settings::types::PythonVersion; use crate::settings::types::PythonVersion;
use crate::{flake8_annotations, flake8_bugbear, flake8_quotes, isort, pep8_naming}; use crate::{
flake8_annotations, flake8_bugbear, flake8_quotes, flake8_tidy_imports, isort, pep8_naming,
};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")] #[serde(deny_unknown_fields, rename_all = "kebab-case")]
@ -25,6 +27,7 @@ pub struct Options {
pub flake8_annotations: Option<flake8_annotations::settings::Options>, pub flake8_annotations: Option<flake8_annotations::settings::Options>,
pub flake8_bugbear: Option<flake8_bugbear::settings::Options>, pub flake8_bugbear: Option<flake8_bugbear::settings::Options>,
pub flake8_quotes: Option<flake8_quotes::settings::Options>, pub flake8_quotes: Option<flake8_quotes::settings::Options>,
pub flake8_tidy_imports: Option<flake8_tidy_imports::settings::Options>,
pub isort: Option<isort::settings::Options>, pub isort: Option<isort::settings::Options>,
pub pep8_naming: Option<pep8_naming::settings::Options>, pub pep8_naming: Option<pep8_naming::settings::Options>,
// Tables are required to go last. // Tables are required to go last.

View file

@ -105,11 +105,12 @@ mod tests {
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
use crate::flake8_quotes::settings::Quote; use crate::flake8_quotes::settings::Quote;
use crate::flake8_tidy_imports::settings::Strictness;
use crate::settings::pyproject::{ use crate::settings::pyproject::{
find_project_root, find_pyproject_toml, parse_pyproject_toml, Options, Pyproject, Tools, find_project_root, find_pyproject_toml, parse_pyproject_toml, Options, Pyproject, Tools,
}; };
use crate::settings::types::PatternPrefixPair; use crate::settings::types::PatternPrefixPair;
use crate::{flake8_bugbear, flake8_quotes, pep8_naming}; use crate::{flake8_bugbear, flake8_quotes, flake8_tidy_imports, pep8_naming};
#[test] #[test]
fn deserialize() -> Result<()> { fn deserialize() -> Result<()> {
@ -148,6 +149,7 @@ mod tests {
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}) })
@ -180,6 +182,7 @@ line-length = 79
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}) })
@ -212,6 +215,7 @@ exclude = ["foo.py"]
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}) })
@ -244,6 +248,7 @@ select = ["E501"]
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}) })
@ -277,6 +282,7 @@ ignore = ["E501"]
flake8_annotations: None, flake8_annotations: None,
flake8_bugbear: None, flake8_bugbear: None,
flake8_quotes: None, flake8_quotes: None,
flake8_tidy_imports: None,
isort: None, isort: None,
pep8_naming: None, pep8_naming: None,
}) })
@ -366,6 +372,9 @@ other-attribute = 1
docstring_quotes: Some(Quote::Double), docstring_quotes: Some(Quote::Double),
avoid_escape: Some(true), avoid_escape: Some(true),
}), }),
flake8_tidy_imports: Some(flake8_tidy_imports::settings::Options {
ban_relative_imports: Some(Strictness::Parents)
}),
isort: None, isort: None,
pep8_naming: Some(pep8_naming::settings::Options { pep8_naming: Some(pep8_naming::settings::Options {
ignore_names: Some(vec![ ignore_names: Some(vec![

View file

@ -7,7 +7,9 @@ use regex::Regex;
use crate::checks::CheckCode; use crate::checks::CheckCode;
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
use crate::settings::types::{FilePattern, PythonVersion}; use crate::settings::types::{FilePattern, PythonVersion};
use crate::{flake8_annotations, flake8_quotes, isort, pep8_naming, Configuration}; use crate::{
flake8_annotations, flake8_quotes, flake8_tidy_imports, isort, pep8_naming, Configuration,
};
/// Struct to render user-facing exclusion patterns. /// Struct to render user-facing exclusion patterns.
#[derive(Debug)] #[derive(Debug)]
@ -50,6 +52,7 @@ pub struct UserConfiguration {
// Plugins // Plugins
pub flake8_annotations: flake8_annotations::settings::Settings, pub flake8_annotations: flake8_annotations::settings::Settings,
pub flake8_quotes: flake8_quotes::settings::Settings, pub flake8_quotes: flake8_quotes::settings::Settings,
pub flake8_tidy_imports: flake8_tidy_imports::settings::Settings,
pub isort: isort::settings::Settings, pub isort: isort::settings::Settings,
pub pep8_naming: pep8_naming::settings::Settings, pub pep8_naming: pep8_naming::settings::Settings,
// Non-settings exposed to the user // Non-settings exposed to the user
@ -95,6 +98,7 @@ impl UserConfiguration {
target_version: configuration.target_version, target_version: configuration.target_version,
flake8_annotations: configuration.flake8_annotations, flake8_annotations: configuration.flake8_annotations,
flake8_quotes: configuration.flake8_quotes, flake8_quotes: configuration.flake8_quotes,
flake8_tidy_imports: configuration.flake8_tidy_imports,
isort: configuration.isort, isort: configuration.isort,
pep8_naming: configuration.pep8_naming, pep8_naming: configuration.pep8_naming,
project_root, project_root,