From 52988bbf2e6f3d05eafcc8c2e13203eb1973c20a Mon Sep 17 00:00:00 2001 From: bluthej Date: Mon, 13 Nov 2023 23:11:51 +0100 Subject: [PATCH] Fix ordering for `force-sort-within-sections` Imports like `from x import y as z` don't have an "asname" for the module, so they were placed before imports like `import x as w` since `None` < `Some(s)` for any string s. The fix is to first sort by `first_alias` since it's `None` for `import x as w`. --- ...orce_sort_within_sections_with_as_names.py | 5 ++++ crates/ruff_linter/src/rules/isort/mod.rs | 1 + ...sort_within_sections_with_as_names.py.snap | 25 +++++++++++++++++++ crates/ruff_linter/src/rules/isort/sorting.rs | 4 +-- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/isort/force_sort_within_sections_with_as_names.py create mode 100644 crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__force_sort_within_sections_force_sort_within_sections_with_as_names.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/isort/force_sort_within_sections_with_as_names.py b/crates/ruff_linter/resources/test/fixtures/isort/force_sort_within_sections_with_as_names.py new file mode 100644 index 0000000000000..2c17bb03ff3ef --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/isort/force_sort_within_sections_with_as_names.py @@ -0,0 +1,5 @@ +import encodings +from datetime import timezone as tz +from datetime import timedelta +import datetime as dt +import datetime diff --git a/crates/ruff_linter/src/rules/isort/mod.rs b/crates/ruff_linter/src/rules/isort/mod.rs index e82700398f810..2b4e09bff0517 100644 --- a/crates/ruff_linter/src/rules/isort/mod.rs +++ b/crates/ruff_linter/src/rules/isort/mod.rs @@ -697,6 +697,7 @@ mod tests { } #[test_case(Path::new("force_sort_within_sections.py"))] + #[test_case(Path::new("force_sort_within_sections_with_as_names.py"))] fn force_sort_within_sections(path: &Path) -> Result<()> { let snapshot = format!("force_sort_within_sections_{}", path.to_string_lossy()); let mut diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__force_sort_within_sections_force_sort_within_sections_with_as_names.py.snap b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__force_sort_within_sections_force_sort_within_sections_with_as_names.py.snap new file mode 100644 index 0000000000000..b14f52ac659df --- /dev/null +++ b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__force_sort_within_sections_force_sort_within_sections_with_as_names.py.snap @@ -0,0 +1,25 @@ +--- +source: crates/ruff_linter/src/rules/isort/mod.rs +--- +force_sort_within_sections_with_as_names.py:1:1: I001 [*] Import block is un-sorted or un-formatted + | +1 | / import encodings +2 | | from datetime import timezone as tz +3 | | from datetime import timedelta +4 | | import datetime as dt +5 | | import datetime + | + = help: Organize imports + +ℹ Safe fix + 1 |+import datetime + 2 |+import datetime as dt + 3 |+from datetime import timedelta + 4 |+from datetime import timezone as tz +1 5 | import encodings +2 |-from datetime import timezone as tz +3 |-from datetime import timedelta +4 |-import datetime as dt +5 |-import datetime + + diff --git a/crates/ruff_linter/src/rules/isort/sorting.rs b/crates/ruff_linter/src/rules/isort/sorting.rs index 5b082b0dabddc..cb6f01f730054 100644 --- a/crates/ruff_linter/src/rules/isort/sorting.rs +++ b/crates/ruff_linter/src/rules/isort/sorting.rs @@ -78,8 +78,8 @@ pub(crate) struct ModuleKey<'a> { force_to_top: Option, maybe_lowercase_name: Option>, module_name: Option>, - asname: Option>, first_alias: Option>, + asname: Option>, } impl<'a> ModuleKey<'a> { @@ -110,8 +110,8 @@ impl<'a> ModuleKey<'a> { force_to_top, maybe_lowercase_name, module_name, - asname, first_alias, + asname, } } }