Sort relative imports by parent level descending (#856)

This commit is contained in:
Charlie Marsh 2022-11-21 13:30:24 -05:00 committed by GitHub
parent f8b49f308d
commit 7bd6db62d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -0,0 +1,4 @@
from .a import a
from ..a import a
from ..b import a
from .b import a

View file

@ -1,3 +1,4 @@
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
@ -381,6 +382,7 @@ fn sort_imports(block: ImportBlock) -> OrderedImportBlock {
// Sort each `StmtKind::ImportFrom` by module key, breaking ties based on
// members.
(
Reverse(import_from.level),
import_from
.module
.as_ref()
@ -477,6 +479,7 @@ mod tests {
#[test_case(Path::new("leading_prefix.py"))]
#[test_case(Path::new("no_reorder_within_section.py"))]
#[test_case(Path::new("order_by_type.py"))]
#[test_case(Path::new("order_relative_imports_by_level.py"))]
#[test_case(Path::new("preserve_comment_order.py"))]
#[test_case(Path::new("preserve_indentation.py"))]
#[test_case(Path::new("reorder_within_section.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: "from ..a import a\nfrom ..b import a\nfrom .a import a\nfrom .b import a\n"
location:
row: 1
column: 0
end_location:
row: 5
column: 0
applied: false