Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
68 changes: 68 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down