From e88093541f9991b1e984dfd7c3f507ccad24ed8a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 5 Dec 2022 18:39:16 -0500 Subject: [PATCH] Avoid wrapping import-star statements (#1089) --- resources/test/fixtures/isort/no_wrap_star.py | 1 + src/isort/format.rs | 11 +++++++++- src/isort/mod.rs | 1 + .../ruff__isort__tests__no_wrap_star.py.snap | 20 +++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 resources/test/fixtures/isort/no_wrap_star.py create mode 100644 src/isort/snapshots/ruff__isort__tests__no_wrap_star.py.snap diff --git a/resources/test/fixtures/isort/no_wrap_star.py b/resources/test/fixtures/isort/no_wrap_star.py new file mode 100644 index 0000000000..a425027625 --- /dev/null +++ b/resources/test/fixtures/isort/no_wrap_star.py @@ -0,0 +1 @@ +from .subscription import * # type: ignore # some very long comment explaining why this needs a type ignore diff --git a/src/isort/format.rs b/src/isort/format.rs index 3c1ea00d32..3ee8b08808 100644 --- a/src/isort/format.rs +++ b/src/isort/format.rs @@ -42,6 +42,15 @@ pub fn format_import_from( force_wrap_aliases: bool, is_first: bool, ) -> String { + if aliases.len() == 1 + && aliases + .iter() + .all(|(alias, _)| alias.name == "*" && alias.asname.is_none()) + { + let (single_line, ..) = format_single_line(import_from, comments, aliases, is_first); + return single_line; + } + // We can only inline if: (1) none of the aliases have atop comments, and (3) // only the last alias (if any) has inline comments. if aliases @@ -58,7 +67,7 @@ pub fn format_import_from( { let (single_line, import_length) = format_single_line(import_from, comments, aliases, is_first); - if import_length <= line_length { + if import_length <= line_length || aliases.iter().any(|(alias, _)| alias.name == "*") { return single_line; } } diff --git a/src/isort/mod.rs b/src/isort/mod.rs index aa9607c74c..4bb556a013 100644 --- a/src/isort/mod.rs +++ b/src/isort/mod.rs @@ -561,6 +561,7 @@ mod tests { #[test_case(Path::new("insert_empty_lines.py"))] #[test_case(Path::new("leading_prefix.py"))] #[test_case(Path::new("no_reorder_within_section.py"))] + #[test_case(Path::new("no_wrap_star.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"))] diff --git a/src/isort/snapshots/ruff__isort__tests__no_wrap_star.py.snap b/src/isort/snapshots/ruff__isort__tests__no_wrap_star.py.snap new file mode 100644 index 0000000000..e69fe97569 --- /dev/null +++ b/src/isort/snapshots/ruff__isort__tests__no_wrap_star.py.snap @@ -0,0 +1,20 @@ +--- +source: src/isort/mod.rs +expression: checks +--- +- kind: UnsortedImports + location: + row: 1 + column: 0 + end_location: + row: 2 + column: 0 + fix: + content: "from .subscription import * # type: ignore # some very long comment explaining why this needs a type ignore\n" + location: + row: 1 + column: 0 + end_location: + row: 2 + column: 0 +