Skip to content

Commit e63ae06

Browse files
Merge pull request #1000 from discordapp/black_newline_before_comment
Add configuration to ensure a newline before each comment
2 parents c8c3302 + 1e78a9a commit e63ae06

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

isort/isort.py

+14
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def _add_straight_imports(
353353

354354
comments_above = self.comments["above"]["straight"].pop(module, None)
355355
if comments_above:
356+
if section_output and self.config.get("ensure_newline_before_comments"):
357+
section_output.append("")
356358
section_output.extend(comments_above)
357359
section_output.extend(
358360
self._add_comments(self.comments["straight"].get(module), idef)
@@ -470,6 +472,10 @@ def _add_from_imports(
470472
module, None
471473
)
472474
if above_comments:
475+
if section_output and self.config.get(
476+
"ensure_newline_before_comments"
477+
):
478+
section_output.append("")
473479
section_output.extend(above_comments)
474480

475481
if (
@@ -521,6 +527,10 @@ def _add_from_imports(
521527
module, None
522528
)
523529
if above_comments:
530+
if section_output and self.config.get(
531+
"ensure_newline_before_comments"
532+
):
533+
section_output.append("")
524534
section_output.extend(above_comments)
525535
section_output.append(self._wrap(single_import_line))
526536
from_imports.remove(from_import)
@@ -598,6 +608,10 @@ def _add_from_imports(
598608
if import_statement:
599609
above_comments = self.comments["above"]["from"].pop(module, None)
600610
if above_comments:
611+
if section_output and self.config.get(
612+
"ensure_newline_before_comments"
613+
):
614+
section_output.append("")
601615
section_output.extend(above_comments)
602616
section_output.append(import_statement)
603617

test_isort.py

+68
Original file line numberDiff line numberDiff line change
@@ -4154,6 +4154,74 @@ def test_isort_keeps_comments_issue_691() -> None:
41544154
assert SortImports(file_contents=test_input).output == expected_output
41554155

41564156

4157+
def test_isort_ensures_blank_line_between_import_and_comment() -> None:
4158+
config = {
4159+
"ensure_newline_before_comments": True,
4160+
"known_one": ["one"],
4161+
"known_two": ["two"],
4162+
"known_three": ["three"],
4163+
"known_four": ["four"],
4164+
"sections": [
4165+
"FUTURE",
4166+
"STDLIB",
4167+
"FIRSTPARTY",
4168+
"THIRDPARTY",
4169+
"LOCALFOLDER",
4170+
"ONE",
4171+
"TWO",
4172+
"THREE",
4173+
"FOUR",
4174+
],
4175+
} # type: Dict[str, Any]
4176+
test_input = (
4177+
"import os\n"
4178+
"# noinspection PyUnresolvedReferences\n"
4179+
"import one.a\n"
4180+
"# noinspection PyUnresolvedReferences\n"
4181+
"import one.b\n"
4182+
"# noinspection PyUnresolvedReferences\n"
4183+
"import two.a as aa\n"
4184+
"# noinspection PyUnresolvedReferences\n"
4185+
"import two.b as bb\n"
4186+
"# noinspection PyUnresolvedReferences\n"
4187+
"from three.a import a\n"
4188+
"# noinspection PyUnresolvedReferences\n"
4189+
"from three.b import b\n"
4190+
"# noinspection PyUnresolvedReferences\n"
4191+
"from four.a import a as aa\n"
4192+
"# noinspection PyUnresolvedReferences\n"
4193+
"from four.b import b as bb\n"
4194+
)
4195+
expected_output = (
4196+
"import os\n"
4197+
"\n"
4198+
"# noinspection PyUnresolvedReferences\n"
4199+
"import one.a\n"
4200+
"\n"
4201+
"# noinspection PyUnresolvedReferences\n"
4202+
"import one.b\n"
4203+
"\n"
4204+
"# noinspection PyUnresolvedReferences\n"
4205+
"import two.a as aa\n"
4206+
"\n"
4207+
"# noinspection PyUnresolvedReferences\n"
4208+
"import two.b as bb\n"
4209+
"\n"
4210+
"# noinspection PyUnresolvedReferences\n"
4211+
"from three.a import a\n"
4212+
"\n"
4213+
"# noinspection PyUnresolvedReferences\n"
4214+
"from three.b import b\n"
4215+
"\n"
4216+
"# noinspection PyUnresolvedReferences\n"
4217+
"from four.a import a as aa\n"
4218+
"\n"
4219+
"# noinspection PyUnresolvedReferences\n"
4220+
"from four.b import b as bb\n"
4221+
)
4222+
assert SortImports(file_contents=test_input, **config).output == expected_output
4223+
4224+
41574225
def test_pyi_formatting_issue_942(tmpdir) -> None:
41584226
test_input = "import os\n\n\ndef my_method():\n"
41594227
expected_py_output = test_input.splitlines()

0 commit comments

Comments
 (0)