Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[isort] Add support for length-sort settings #8841

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from mediuuuuuuuuuuum import a
from short import b
from loooooooooooooooooooooog import c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from module1 import (
loooooooooooooong,
σηορτ,
mediuuuuum,
shoort,
looooooooooooooong,
μεδιυυυυυμ,
short,
mediuuuuuum,
λοοοοοοοοοοοοοονγ,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import loooooooooooooong
import mediuuuuuum
import short
import σηορτ
import shoort
import mediuuuuum
import λοοοοοοοοοοοοοονγ
import μεδιυυυυυμ
import looooooooooooooong
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mediuuuuuum
import short
import looooooooooooooooong
from looooooooooooooong import a
from mediuuuum import c
from short import b
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import mediuuuuuumb
import short
import looooooooooooooooong
import mediuuuuuuma
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ..looooooooooooooong import a
from ...mediuuuum import b
from .short import c
from ....short import c
from . import d
from .mediuuuum import a
from ......short import b
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from looooooooooooooong import a
from mediuuuum import *
from short import *
43 changes: 43 additions & 0 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,47 @@ mod tests {
assert_messages!(diagnostics);
Ok(())
}

#[test_case(Path::new("length_sort_straight_imports.py"))]
#[test_case(Path::new("length_sort_from_imports.py"))]
#[test_case(Path::new("length_sort_straight_and_from_imports.py"))]
#[test_case(Path::new("length_sort_non_ascii_members.py"))]
#[test_case(Path::new("length_sort_non_ascii_modules.py"))]
#[test_case(Path::new("length_sort_with_relative_imports.py"))]
fn length_sort(path: &Path) -> Result<()> {
let snapshot = format!("length_sort__{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("isort").join(path).as_path(),
&LinterSettings {
isort: super::settings::Settings {
length_sort: true,
..super::settings::Settings::default()
},
src: vec![test_resource_path("fixtures/isort")],
..LinterSettings::for_rule(Rule::UnsortedImports)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("length_sort_straight_imports.py"))]
#[test_case(Path::new("length_sort_from_imports.py"))]
#[test_case(Path::new("length_sort_straight_and_from_imports.py"))]
fn length_sort_straight(path: &Path) -> Result<()> {
let snapshot = format!("length_sort_straight__{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("isort").join(path).as_path(),
&LinterSettings {
isort: super::settings::Settings {
length_sort_straight: true,
..super::settings::Settings::default()
},
src: vec![test_resource_path("fixtures/isort")],
..LinterSettings::for_rule(Rule::UnsortedImports)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}
15 changes: 11 additions & 4 deletions crates/ruff_linter/src/rules/isort/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,27 @@ pub(crate) fn order_imports<'a>(
.map(Import)
.chain(from_imports.map(ImportFrom))
.sorted_by_cached_key(|import| match import {
Import((alias, _)) => {
ModuleKey::from_module(Some(alias.name), alias.asname, None, None, settings)
}
Import((alias, _)) => ModuleKey::from_module(
Some(alias.name),
alias.asname,
None,
None,
settings,
true,
),
ImportFrom((import_from, _, _, aliases)) => ModuleKey::from_module(
import_from.module,
None,
import_from.level,
aliases.first().map(|(alias, _)| (alias.name, alias.asname)),
settings,
false,
),
})
.collect()
} else {
let ordered_straight_imports = straight_imports.sorted_by_cached_key(|(alias, _)| {
ModuleKey::from_module(Some(alias.name), alias.asname, None, None, settings)
ModuleKey::from_module(Some(alias.name), alias.asname, None, None, settings, true)
});
let ordered_from_imports =
from_imports.sorted_by_cached_key(|(import_from, _, _, aliases)| {
Expand All @@ -80,6 +86,7 @@ pub(crate) fn order_imports<'a>(
import_from.level,
aliases.first().map(|(alias, _)| (alias.name, alias.asname)),
settings,
false,
)
});
if settings.from_first {
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_linter/src/rules/isort/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Settings {
pub section_order: Vec<ImportSection>,
pub no_sections: bool,
pub from_first: bool,
pub length_sort: bool,
pub length_sort_straight: bool,
}

impl Default for Settings {
Expand Down Expand Up @@ -86,6 +88,8 @@ impl Default for Settings {
section_order: ImportType::iter().map(ImportSection::Known).collect(),
no_sections: false,
from_first: false,
length_sort: false,
length_sort_straight: false,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_from_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from mediuuuuuuuuuuum import a
2 | | from short import b
3 | | from loooooooooooooooooooooog import c
|
= help: Organize imports

ℹ Safe fix
1 |+from short import b
1 2 | from mediuuuuuuuuuuum import a
2 |-from short import b
3 3 | from loooooooooooooooooooooog import c


Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_non_ascii_members.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from module1 import (
2 | | loooooooooooooong,
3 | | σηορτ,
4 | | mediuuuuum,
5 | | shoort,
6 | | looooooooooooooong,
7 | | μεδιυυυυυμ,
8 | | short,
9 | | mediuuuuuum,
10 | | λοοοοοοοοοοοοοονγ,
11 | | )
|
= help: Organize imports

ℹ Safe fix
1 1 | from module1 import (
2 |- loooooooooooooong,
2 |+ short,
3 3 | σηορτ,
4 |+ shoort,
4 5 | mediuuuuum,
5 |- shoort,
6 |- looooooooooooooong,
7 6 | μεδιυυυυυμ,
8 |- short,
9 7 | mediuuuuuum,
8 |+ loooooooooooooong,
10 9 | λοοοοοοοοοοοοοονγ,
10 |+ looooooooooooooong,
11 11 | )


Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_non_ascii_modules.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import loooooooooooooong
2 | | import mediuuuuuum
3 | | import short
4 | | import σηορτ
5 | | import shoort
6 | | import mediuuuuum
7 | | import λοοοοοοοοοοοοοονγ
8 | | import μεδιυυυυυμ
9 | | import looooooooooooooong
|
= help: Organize imports

ℹ Safe fix
1 |-import loooooooooooooong
2 |-import mediuuuuuum
3 1 | import short
4 2 | import σηορτ
5 3 | import shoort
6 4 | import mediuuuuum
5 |+import μεδιυυυυυμ
6 |+import mediuuuuuum
7 |+import loooooooooooooong
7 8 | import λοοοοοοοοοοοοοονγ
8 |-import μεδιυυυυυμ
9 9 | import looooooooooooooong


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_straight_and_from_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import mediuuuuuum
2 | | import short
3 | | import looooooooooooooooong
4 | | from looooooooooooooong import a
5 | | from mediuuuum import c
6 | | from short import b
|
= help: Organize imports

ℹ Safe fix
1 |+import short
1 2 | import mediuuuuuum
2 |-import short
3 3 | import looooooooooooooooong
4 |-from looooooooooooooong import a
4 |+from short import b
5 5 | from mediuuuum import c
6 |-from short import b
6 |+from looooooooooooooong import a


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_straight_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import mediuuuuuumb
2 | | import short
3 | | import looooooooooooooooong
4 | | import mediuuuuuuma
|
= help: Organize imports

ℹ Safe fix
1 |+import short
2 |+import mediuuuuuuma
1 3 | import mediuuuuuumb
2 |-import short
3 4 | import looooooooooooooooong
4 |-import mediuuuuuuma


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_with_relative_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from ..looooooooooooooong import a
2 | | from ...mediuuuum import b
3 | | from .short import c
4 | | from ....short import c
5 | | from . import d
6 | | from .mediuuuum import a
7 | | from ......short import b
|
= help: Organize imports

ℹ Safe fix
1 |-from ..looooooooooooooong import a
2 |-from ...mediuuuum import b
1 |+from . import d
3 2 | from .short import c
4 3 | from ....short import c
5 |-from . import d
6 4 | from .mediuuuum import a
7 5 | from ......short import b
6 |+from ...mediuuuum import b
7 |+from ..looooooooooooooong import a


Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_from_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from mediuuuuuuuuuuum import a
2 | | from short import b
3 | | from loooooooooooooooooooooog import c
|
= help: Organize imports

ℹ Safe fix
1 |+from loooooooooooooooooooooog import c
1 2 | from mediuuuuuuuuuuum import a
2 3 | from short import b
3 |-from loooooooooooooooooooooog import c


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_straight_and_from_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import mediuuuuuum
2 | | import short
3 | | import looooooooooooooooong
4 | | from looooooooooooooong import a
5 | | from mediuuuum import c
6 | | from short import b
|
= help: Organize imports

ℹ Safe fix
1 |+import short
1 2 | import mediuuuuuum
2 |-import short
3 3 | import looooooooooooooooong
4 4 | from looooooooooooooong import a
5 5 | from mediuuuum import c


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
length_sort_straight_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import mediuuuuuumb
2 | | import short
3 | | import looooooooooooooooong
4 | | import mediuuuuuuma
|
= help: Organize imports

ℹ Safe fix
1 |+import short
2 |+import mediuuuuuuma
1 3 | import mediuuuuuumb
2 |-import short
3 4 | import looooooooooooooooong
4 |-import mediuuuuuuma


Loading
Loading