Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/1389/ensure new line before comments bug #1390

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ def sorted_imports(
for line in new_section_output:
comments = getattr(line, "comments", ())
if comments:
if (
config.ensure_newline_before_comments
and section_output
and section_output[-1]
):
section_output.append("")
section_output.extend(comments)
section_output.append(str(line))

Expand Down Expand Up @@ -172,6 +166,9 @@ def sorted_imports(
else:
pending_lines_before = pending_lines_before or not no_lines_before

if config.ensure_newline_before_comments:
Copy link
Collaborator Author

@sztamas sztamas Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure whether we want to insert an empty line before on a comment above the first import as well.
It looks a bit strange to me to have it there, so I opted for not having an empty line in this special case.

In case you want it just move these 2 lines below the stripping of empty lines that follows next. I tested that it works there as well.

output = _ensure_newline_before_comment(output)

while output and output[-1].strip() == "":
output.pop() # pragma: no cover
while output and output[0].strip() == "":
Expand Down Expand Up @@ -296,8 +293,6 @@ def _with_from_imports(
comments = parsed.categorized_comments["from"].pop(module, ())
above_comments = parsed.categorized_comments["above"]["from"].pop(module, None)
if above_comments:
if new_section_output and config.ensure_newline_before_comments:
new_section_output.append("")
new_section_output.extend(above_comments)

if "*" in from_imports and config.combine_star:
Expand Down Expand Up @@ -550,3 +545,16 @@ def __new__(cls, value, comments):
instance = super().__new__(cls, value) # type: ignore
instance.comments = comments
return instance


def _ensure_newline_before_comment(output):
new_output: List[str] = []

def is_comment(line):
return line and line.startswith("#")

for line, prev_line in zip(output, [None] + output):
if is_comment(line) and prev_line != "" and not is_comment(prev_line):
new_output.append("")
new_output.append(line)
return new_output
1 change: 1 addition & 0 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3816,6 +3816,7 @@ 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,
"lines_between_sections": 0,
"known_one": ["one"],
"known_two": ["two"],
"known_three": ["three"],
Expand Down