Add a separate local folder category for imports (#690)

This commit is contained in:
Charlie Marsh 2022-11-11 22:12:48 -05:00 committed by GitHub
parent 5a8b7c1d20
commit 048a13c795
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 6 deletions

View file

@ -0,0 +1,2 @@
[tool.ruff]
line-length = 88

View file

@ -0,0 +1,4 @@
import sys
import leading_prefix
import os
from . import leading_prefix

View file

@ -12,16 +12,20 @@ pub enum ImportType {
StandardLibrary, StandardLibrary,
ThirdParty, ThirdParty,
FirstParty, FirstParty,
LocalFolder,
} }
pub fn categorize( pub fn categorize(
module_base: &str, module_base: &str,
level: &Option<usize>,
src: &[PathBuf], src: &[PathBuf],
known_first_party: &BTreeSet<String>, known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>, known_third_party: &BTreeSet<String>,
extra_standard_library: &BTreeSet<String>, extra_standard_library: &BTreeSet<String>,
) -> ImportType { ) -> ImportType {
if known_first_party.contains(module_base) { if level.map(|level| level > 0).unwrap_or(false) {
ImportType::LocalFolder
} else if known_first_party.contains(module_base) {
ImportType::FirstParty ImportType::FirstParty
} else if known_third_party.contains(module_base) { } else if known_third_party.contains(module_base) {
ImportType::ThirdParty ImportType::ThirdParty
@ -31,14 +35,12 @@ pub fn categorize(
import_type.clone() import_type.clone()
} else if KNOWN_STANDARD_LIBRARY.contains(module_base) { } else if KNOWN_STANDARD_LIBRARY.contains(module_base) {
ImportType::StandardLibrary ImportType::StandardLibrary
} else { } else if find_local(src, module_base) {
if find_local(src, module_base) {
ImportType::FirstParty ImportType::FirstParty
} else { } else {
ImportType::ThirdParty ImportType::ThirdParty
} }
} }
}
static STATIC_CLASSIFICATIONS: Lazy<BTreeMap<&'static str, ImportType>> = Lazy::new(|| { static STATIC_CLASSIFICATIONS: Lazy<BTreeMap<&'static str, ImportType>> = Lazy::new(|| {
BTreeMap::from([ BTreeMap::from([

View file

@ -62,6 +62,7 @@ fn categorize_imports<'a>(
for alias in block.import { for alias in block.import {
let import_type = categorize( let import_type = categorize(
&alias.module_base(), &alias.module_base(),
&None,
src, src,
known_first_party, known_first_party,
known_third_party, known_third_party,
@ -77,6 +78,7 @@ fn categorize_imports<'a>(
for (import_from, aliases) in block.import_from { for (import_from, aliases) in block.import_from {
let classification = categorize( let classification = categorize(
&import_from.module_base(), &import_from.module_base(),
import_from.level,
src, src,
known_first_party, known_first_party,
known_third_party, known_third_party,
@ -119,6 +121,7 @@ pub fn sort_imports(
ImportType::StandardLibrary, ImportType::StandardLibrary,
ImportType::ThirdParty, ImportType::ThirdParty,
ImportType::FirstParty, ImportType::FirstParty,
ImportType::LocalFolder,
] { ] {
if let Some(import_block) = block_by_type.get(&import_type) { if let Some(import_block) = block_by_type.get(&import_type) {
// Add a blank line between every section. // Add a blank line between every section.
@ -218,6 +221,7 @@ mod tests {
#[test_case(Path::new("reorder_within_section.py"))] #[test_case(Path::new("reorder_within_section.py"))]
#[test_case(Path::new("separate_first_party_imports.py"))] #[test_case(Path::new("separate_first_party_imports.py"))]
#[test_case(Path::new("separate_future_imports.py"))] #[test_case(Path::new("separate_future_imports.py"))]
#[test_case(Path::new("separate_local_folder_imports.py"))]
#[test_case(Path::new("separate_third_party_imports.py"))] #[test_case(Path::new("separate_third_party_imports.py"))]
#[test_case(Path::new("skip.py"))] #[test_case(Path::new("skip.py"))]
#[test_case(Path::new("trailing_suffix.py"))] #[test_case(Path::new("trailing_suffix.py"))]

View file

@ -0,0 +1,22 @@
---
source: src/isort/mod.rs
expression: checks
---
- kind: UnsortedImports
location:
row: 1
column: 0
end_location:
row: 5
column: 0
fix:
patch:
content: "import os\nimport sys\n\nimport leading_prefix\n\nfrom . import leading_prefix\n"
location:
row: 1
column: 0
end_location:
row: 5
column: 0
applied: false