Repository: timothycrosley/isort
Pull request #1000 (2019-08-18 17:49:24)

Add configuration to ensure a newline before each comment
This adds a `ensure_newline_before_comments` configuration that matches the format that `black` imposes (for better or worse) and allows `isort` to be used with `black` without any thrashing/conflicts with regards to blank lines before comments.

Should help address https://github.com/psf/black/issues/251


Patch:
diff --git a/isort/isort.py b/isort/isort.py
index d6084ba0..48f65453 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -353,6 +353,8 @@ class _SortImports:
 
             comments_above = self.comments["above"]["straight"].pop(module, None)
             if comments_above:
+                if section_output and self.config.get("ensure_newline_before_comments"):
+                    section_output.append("")
                 section_output.extend(comments_above)
             section_output.extend(
                 self._add_comments(self.comments["straight"].get(module), idef)
@@ -470,6 +472,10 @@ class _SortImports:
                             module, None
                         )
                         if above_comments:
+                            if section_output and self.config.get(
+                                "ensure_newline_before_comments"
+                            ):
+                                section_output.append("")
                             section_output.extend(above_comments)
 
                         if (
@@ -521,6 +527,10 @@ class _SortImports:
                                 module, None
                             )
                             if above_comments:
+                                if section_output and self.config.get(
+                                    "ensure_newline_before_comments"
+                                ):
+                                    section_output.append("")
                                 section_output.extend(above_comments)
                             section_output.append(self._wrap(single_import_line))
                             from_imports.remove(from_import)
@@ -598,6 +608,10 @@ class _SortImports:
                 if import_statement:
                     above_comments = self.comments["above"]["from"].pop(module, None)
                     if above_comments:
+                        if section_output and self.config.get(
+                            "ensure_newline_before_comments"
+                        ):
+                            section_output.append("")
                         section_output.extend(above_comments)
                     section_output.append(import_statement)
 


Test patch:
diff --git a/test_isort.py b/test_isort.py
index 5e38a808..ae5eee89 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -4154,6 +4154,74 @@ def test_isort_keeps_comments_issue_691() -> None:
     assert SortImports(file_contents=test_input).output == expected_output
 
 
+def test_isort_ensures_blank_line_between_import_and_comment() -> None:
+    config = {
+        "ensure_newline_before_comments": True,
+        "known_one": ["one"],
+        "known_two": ["two"],
+        "known_three": ["three"],
+        "known_four": ["four"],
+        "sections": [
+            "FUTURE",
+            "STDLIB",
+            "FIRSTPARTY",
+            "THIRDPARTY",
+            "LOCALFOLDER",
+            "ONE",
+            "TWO",
+            "THREE",
+            "FOUR",
+        ],
+    }  # type: Dict[str, Any]
+    test_input = (
+        "import os\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import one.a\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import one.b\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import two.a as aa\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import two.b as bb\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from three.a import a\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from three.b import b\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from four.a import a as aa\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from four.b import b as bb\n"
+    )
+    expected_output = (
+        "import os\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import one.a\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import one.b\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import two.a as aa\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "import two.b as bb\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from three.a import a\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from three.b import b\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from four.a import a as aa\n"
+        "\n"
+        "# noinspection PyUnresolvedReferences\n"
+        "from four.b import b as bb\n"
+    )
+    assert SortImports(file_contents=test_input, **config).output == expected_output
+
+
 def test_pyi_formatting_issue_942(tmpdir) -> None:
     test_input = "import os\n\n\ndef my_method():\n"
     expected_py_output = test_input.splitlines()