Skip to content

Commit

Permalink
Use unicode_width to sort by length + add tests
Browse files Browse the repository at this point in the history
The `UnicodeWidthStr` trait yields "displayed width of Unicode strings".

Some tests with non-ASCII characters were added.
  • Loading branch information
bluthej committed Nov 26, 2023
1 parent 32eff8c commit 0cefa55
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
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,3 @@
from looooooooooooooong import a
from mediuuuum import *
from short import *
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,8 @@ mod tests {
#[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"))]
fn length_sort(path: &Path) -> Result<()> {
let snapshot = format!("length_sort__{}", path.to_string_lossy());
let diagnostics = test_path(
Expand Down
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

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

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


5 changes: 3 additions & 2 deletions crates/ruff_linter/src/rules/isort/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{borrow::Cow, cmp::Ordering, cmp::Reverse};

use natord;
use unicode_width::UnicodeWidthStr;

use ruff_python_stdlib::str;

Expand Down Expand Up @@ -103,7 +104,7 @@ impl<'a> ModuleKey<'a> {

let maybe_length = (settings.length_sort
|| (settings.length_sort_straight && straight_import))
.then_some(name.map(str::len).unwrap_or_default());
.then_some(name.map(str::width).unwrap_or_default());

let maybe_lowercase_name = name.and_then(|name| {
(!settings.case_sensitive).then_some(NatOrdStr(maybe_lowercase(name)))
Expand Down Expand Up @@ -146,7 +147,7 @@ impl<'a> MemberKey<'a> {
let member_type = settings
.order_by_type
.then_some(member_type(name, settings));
let maybe_length = settings.length_sort.then_some(name.len());
let maybe_length = settings.length_sort.then_some(name.width());
let maybe_lowercase_name =
(!settings.case_sensitive).then_some(NatOrdStr(maybe_lowercase(name)));
let module_name = NatOrdStr::from(name);
Expand Down

0 comments on commit 0cefa55

Please sign in to comment.