From b26bc51b8a3b378e270a92cc4014fc13ba32be8f Mon Sep 17 00:00:00 2001 From: Danny Weinberg Date: Sun, 18 Aug 2019 10:47:33 -0700 Subject: [PATCH 1/6] 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 --- isort/isort.py | 14 ++++++++++++++ test_isort.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/isort/isort.py b/isort/isort.py index d6084ba00..48f65453d 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -353,6 +353,8 @@ def _add_straight_imports( 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 @@ def _add_from_imports( 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 @@ def _add_from_imports( 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 @@ def _add_from_imports( 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) diff --git a/test_isort.py b/test_isort.py index 3d87964f1..06a1fef2c 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4178,6 +4178,38 @@ 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} # type: Dict[str, Any] + test_input = ( + "import os\n" + "import sys\n" + "# noinspection PyUnresolvedReferences\n" + "import a\n" + "import b\n" + "# noinspection PyUnresolvedReferences\n" + "import c\n" + "from a import a\n" + "# noinspection PyUnresolvedReferences\n" + "from b import b\n" + ) + expected_output = ( + "import os\n" + "import sys\n" + "\n" + "# noinspection PyUnresolvedReferences\n" + "import a\n" + "import b\n" + "\n" + "# noinspection PyUnresolvedReferences\n" + "import c\n" + "from a import a\n" + "\n" + "# noinspection PyUnresolvedReferences\n" + "from b import b\n" + ) + assert SortImports(file_contents=test_input, **config).output == expected_output + + def test_pyi_formatting_issue_942(tmpdir): test_input = "import os\n" "\n" "\n" "def my_method():\n" expected_py_output = test_input.splitlines() From a7a606ed37f1f11237691ecc2277824bed945952 Mon Sep 17 00:00:00 2001 From: Danny Weinberg Date: Sun, 18 Aug 2019 11:21:50 -0700 Subject: [PATCH 2/6] Add some more test cases to increase coverage. --- test_isort.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test_isort.py b/test_isort.py index 06a1fef2c..2dc4da8c6 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4188,9 +4188,13 @@ def test_isort_ensures_blank_line_between_import_and_comment() -> None: "import b\n" "# noinspection PyUnresolvedReferences\n" "import c\n" + "# noinspection PyUnresolvedReferences\n" + "import d as dd\n" "from a import a\n" "# noinspection PyUnresolvedReferences\n" "from b import b\n" + "# noinspection PyUnresolvedReferences\n" + "from c import c as cc\n" ) expected_output = ( "import os\n" @@ -4202,10 +4206,16 @@ def test_isort_ensures_blank_line_between_import_and_comment() -> None: "\n" "# noinspection PyUnresolvedReferences\n" "import c\n" + "\n" + "# noinspection PyUnresolvedReferences\n" + "import d as dd\n" "from a import a\n" "\n" "# noinspection PyUnresolvedReferences\n" "from b import b\n" + "\n" + "# noinspection PyUnresolvedReferences\n" + "from c import c as cc\n" ) assert SortImports(file_contents=test_input, **config).output == expected_output From ba5d61169d2ab6fed69a8a55c1879a1c53abb61a Mon Sep 17 00:00:00 2001 From: Danny Weinberg Date: Mon, 19 Aug 2019 10:40:35 -0700 Subject: [PATCH 3/6] Expand test more to test both beginning-of-section and middle-of-section logic. --- test_isort.py | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/test_isort.py b/test_isort.py index 2dc4da8c6..d38bcd390 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4179,43 +4179,59 @@ def test_isort_keeps_comments_issue_691() -> None: def test_isort_ensures_blank_line_between_import_and_comment() -> None: - config = {"ensure_newline_before_comments": True} # type: Dict[str, Any] + 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" - "import sys\n" "# noinspection PyUnresolvedReferences\n" - "import a\n" - "import b\n" + "import one.a\n" "# noinspection PyUnresolvedReferences\n" - "import c\n" + "import one.b\n" "# noinspection PyUnresolvedReferences\n" - "import d as dd\n" - "from a import a\n" + "import two.a as aa\n" "# noinspection PyUnresolvedReferences\n" - "from b import b\n" + "import two.b as bb\n" "# noinspection PyUnresolvedReferences\n" - "from c import c as cc\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" - "import sys\n" "\n" "# noinspection PyUnresolvedReferences\n" - "import a\n" - "import b\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" - "import c\n" + "from three.a import a\n" "\n" "# noinspection PyUnresolvedReferences\n" - "import d as dd\n" - "from a import a\n" + "from three.b import b\n" "\n" "# noinspection PyUnresolvedReferences\n" - "from b import b\n" + "from four.a import a as aa\n" "\n" "# noinspection PyUnresolvedReferences\n" - "from c import c as cc\n" + "from four.b import b as bb\n" ) assert SortImports(file_contents=test_input, **config).output == expected_output From 29e35710cb1554775f782f0ed8d914169947209a Mon Sep 17 00:00:00 2001 From: Danny Weinberg Date: Mon, 19 Aug 2019 10:49:38 -0700 Subject: [PATCH 4/6] Fix black formatting. --- test_isort.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test_isort.py b/test_isort.py index d38bcd390..1f01583ce 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4185,7 +4185,17 @@ def test_isort_ensures_blank_line_between_import_and_comment() -> None: "known_two": ["two"], "known_three": ["three"], "known_four": ["four"], - "sections": ["FUTURE", "STDLIB", "FIRSTPARTY", "THIRDPARTY", "LOCALFOLDER", "ONE", "TWO", "THREE", "FOUR"], + "sections": [ + "FUTURE", + "STDLIB", + "FIRSTPARTY", + "THIRDPARTY", + "LOCALFOLDER", + "ONE", + "TWO", + "THREE", + "FOUR", + ], } # type: Dict[str, Any] test_input = ( "import os\n" From b6f6d51cf2bec1b622752b4d489433ce0f8e86ba Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Wed, 11 Sep 2019 20:49:34 -0700 Subject: [PATCH 5/6] Update test_isort.py Remove blank lines per black --- test_isort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_isort.py b/test_isort.py index 3c19a2e71..822269bad 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4224,8 +4224,8 @@ def test_isort_ensures_blank_line_between_import_and_comment() -> None: def test_pyi_formatting_issue_942(tmpdir): test_input = "import os\n" "\n" "\n" "def my_method():\n" - - + + def test_pyi_formatting_issue_942(tmpdir) -> None: test_input = "import os\n\n\ndef my_method():\n" expected_py_output = test_input.splitlines() From 1e78a9acf3110e1f9721feb591f89a451fc9876a Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Wed, 11 Sep 2019 20:56:21 -0700 Subject: [PATCH 6/6] Update test_isort.py Fix method redefinition due to bad merge --- test_isort.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test_isort.py b/test_isort.py index 822269bad..ae5eee892 100644 --- a/test_isort.py +++ b/test_isort.py @@ -4222,10 +4222,6 @@ def test_isort_ensures_blank_line_between_import_and_comment() -> None: assert SortImports(file_contents=test_input, **config).output == expected_output -def test_pyi_formatting_issue_942(tmpdir): - test_input = "import os\n" "\n" "\n" "def my_method():\n" - - def test_pyi_formatting_issue_942(tmpdir) -> None: test_input = "import os\n\n\ndef my_method():\n" expected_py_output = test_input.splitlines()