From 5ac5b69e9fc92d6094012a2a0935c00ed5fc44ed Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Mon, 30 Jan 2023 15:36:19 -0500 Subject: [PATCH] [`I001`] fix isort for files with tab-based indentation (#2361) This PR fixes two related issues with using isort on files using tabs for indentation: - Multiline imports are never considered correctly formatted, since the comparison with the generated code will always fail. - Using autofix generates code that can have mixed indentation in the same line, for imports that are within nested blocks. --- .../test/fixtures/isort/preserve_tabs.py | 29 +++++++++++++++++++ src/rules/isort/format.rs | 7 ++--- src/rules/isort/mod.rs | 1 + ...rules__isort__tests__preserve_tabs.py.snap | 6 ++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 resources/test/fixtures/isort/preserve_tabs.py create mode 100644 src/rules/isort/snapshots/ruff__rules__isort__tests__preserve_tabs.py.snap diff --git a/resources/test/fixtures/isort/preserve_tabs.py b/resources/test/fixtures/isort/preserve_tabs.py new file mode 100644 index 0000000000..3b3122e95f --- /dev/null +++ b/resources/test/fixtures/isort/preserve_tabs.py @@ -0,0 +1,29 @@ +from numpy import ( + cos, + int8, + int16, + int32, + int64, + sin, + tan, + uint8, + uint16, + uint32, + uint64, +) + +if True: + # inside nested block + from numpy import ( + cos, + int8, + int16, + int32, + int64, + sin, + tan, + uint8, + uint16, + uint32, + uint64, + ) diff --git a/src/rules/isort/format.rs b/src/rules/isort/format.rs index f95a7865c5..520bf18581 100644 --- a/src/rules/isort/format.rs +++ b/src/rules/isort/format.rs @@ -1,9 +1,6 @@ use super::types::{AliasData, CommentSet, ImportFromData, Importable}; use crate::source_code::Stylist; -// Hard-code four-space indentation for the imports themselves, to match Black. -const INDENT: &str = " "; - // Guess a capacity to use for string allocation. const CAPACITY: usize = 200; @@ -174,11 +171,11 @@ fn format_multi_line( for (AliasData { name, asname }, comments) in aliases { for comment in &comments.atop { - output.push_str(INDENT); + output.push_str(stylist.indentation()); output.push_str(comment); output.push_str(stylist.line_ending()); } - output.push_str(INDENT); + output.push_str(stylist.indentation()); if let Some(asname) = asname { output.push_str(name); output.push_str(" as "); diff --git a/src/rules/isort/mod.rs b/src/rules/isort/mod.rs index c222c7a35d..133105e4f2 100644 --- a/src/rules/isort/mod.rs +++ b/src/rules/isort/mod.rs @@ -714,6 +714,7 @@ mod tests { #[test_case(Path::new("preserve_comment_order.py"))] #[test_case(Path::new("preserve_import_star.py"))] #[test_case(Path::new("preserve_indentation.py"))] + #[test_case(Path::new("preserve_tabs.py"))] #[test_case(Path::new("relative_imports_order.py"))] #[test_case(Path::new("reorder_within_section.py"))] #[test_case(Path::new("separate_first_party_imports.py"))] diff --git a/src/rules/isort/snapshots/ruff__rules__isort__tests__preserve_tabs.py.snap b/src/rules/isort/snapshots/ruff__rules__isort__tests__preserve_tabs.py.snap new file mode 100644 index 0000000000..40d8799b10 --- /dev/null +++ b/src/rules/isort/snapshots/ruff__rules__isort__tests__preserve_tabs.py.snap @@ -0,0 +1,6 @@ +--- +source: src/rules/isort/mod.rs +expression: diagnostics +--- +[] +