Repository: timothycrosley/isort
Pull request #1023 (2019-09-28 00:36:31)

Fix wrapped imports with comments and trailing comma


Patch:
diff --git a/isort/isort.py b/isort/isort.py
index 48f65453..44e3fbdd 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -273,7 +273,11 @@ class _SortImports:
                 ) and not line_without_comment.strip().startswith(splitter):
                     line_parts = re.split(exp, line_without_comment)
                     if comment:
-                        line_parts[-1] = "{}#{}".format(line_parts[-1], comment)
+                        line_parts[-1] = "{}{}  #{}".format(
+                            line_parts[-1].strip(),
+                            "," if self.config["include_trailing_comma"] else "",
+                            comment,
+                        )
                     next_line = []
                     while (len(line) + 2) > (
                         self.config["wrap_length"] or self.config["line_length"]
@@ -295,7 +299,9 @@ class _SortImports:
                                 splitter,
                                 self.line_separator,
                                 cont_line,
-                                "," if self.config["include_trailing_comma"] else "",
+                                ","
+                                if self.config["include_trailing_comma"] and not comment
+                                else "",
                                 self.line_separator
                                 if wrap_mode
                                 in {
@@ -1285,8 +1291,9 @@ class _SortImports:
                     placed_module = self.place_module(import_from)
                     if self.config["verbose"]:
                         print(
-                            "from-type place_module for %s returned %s"
-                            % (import_from, placed_module)
+                            "from-type place_module for {} returned {}".format(
+                                import_from, placed_module
+                            )
                         )
                     if placed_module == "":
                         print(
@@ -1391,8 +1398,9 @@ class _SortImports:
                         placed_module = self.place_module(module)
                         if self.config["verbose"]:
                             print(
-                                "else-type place_module for %s returned %s"
-                                % (module, placed_module)
+                                "else-type place_module for {} returned {}".format(
+                                    module, placed_module
+                                )
                             )
                         if placed_module == "":
                             print(


Test patch:
diff --git a/test_isort.py b/test_isort.py
index ae5eee89..ad72290b 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -1523,6 +1523,26 @@ def test_include_trailing_comma() -> None:
         "from third_party import (\n    lib1,\n)\n"
     )
 
+    trailing_comma_with_comment = "from six.moves.urllib.parse import urlencode  # pylint: disable=no-name-in-module,import-error"
+    expected_trailing_comma_with_comment = "from six.moves.urllib.parse import (\n    urlencode,  # pylint: disable=no-name-in-module,import-error\n)\n"
+    trailing_comma_with_comment = SortImports(
+        file_contents=trailing_comma_with_comment,
+        line_length=80,
+        multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
+        include_trailing_comma=True,
+        use_parentheses=True,
+    ).output
+    assert trailing_comma_with_comment == expected_trailing_comma_with_comment
+    # The next time around, it should be equal
+    trailing_comma_with_comment = SortImports(
+        file_contents=trailing_comma_with_comment,
+        line_length=80,
+        multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
+        include_trailing_comma=True,
+        use_parentheses=True,
+    ).output
+    assert trailing_comma_with_comment == expected_trailing_comma_with_comment
+
 
 def test_similar_to_std_library() -> None:
     """Test to ensure modules that are named similarly to a standard library import don't end up clobbered"""