Skip to content

Commit

Permalink
[ruff F401 #10390 #10391] categorize imports that this rule will appl…
Browse files Browse the repository at this point in the history
…y to
  • Loading branch information
plredmond committed Apr 29, 2024
1 parent 04a9228 commit adc5ecf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ruff_text_size::{Ranged, TextRange};
use crate::checkers::ast::Checker;
use crate::fix;
use crate::registry::Rule;
use crate::rules::isort::{categorize as categorize_original, ImportSection, ImportType};

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum UnusedImportContext {
Expand Down Expand Up @@ -112,6 +113,34 @@ impl Violation for UnusedImport {
}
}

/// Categorize the import as stdlib, first party, third party, or return `None` if this rule should
/// not apply (e.g. `from __future__ …`).
fn categorize(checker: &Checker, qualified_name: &str) -> Option<ImportType> {
match categorize_original(
qualified_name,
None,
&checker.settings.src,
checker.package(),
checker.settings.isort.detect_same_package,
&checker.settings.isort.known_modules,
checker.settings.target_version,
checker.settings.isort.no_sections,
&checker.settings.isort.section_order,
&checker.settings.isort.default_section,
) {
// this rule doesn't apply
ImportSection::Known(ImportType::Future) => None,
// stdlib
ImportSection::Known(ImportType::StandardLibrary) => Some(ImportType::StandardLibrary),
// first party
ImportSection::Known(ImportType::FirstParty) => Some(ImportType::FirstParty),
ImportSection::Known(ImportType::LocalFolder) => Some(ImportType::FirstParty),
// third party
ImportSection::Known(ImportType::ThirdParty) => Some(ImportType::ThirdParty),
ImportSection::UserDefined(_) => Some(ImportType::ThirdParty),
}
}

pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) {
// Collect all unused imports by statement.
let mut unused: FxHashMap<(NodeId, Exceptions), Vec<ImportBinding>> = FxHashMap::default();
Expand Down

0 comments on commit adc5ecf

Please sign in to comment.