Skip to content

Commit

Permalink
Fixed #1296: Force_single_line setting removes immediately following …
Browse files Browse the repository at this point in the history
…comment line.
  • Loading branch information
timothycrosley committed Jul 8, 2020
1 parent 1685beb commit 65e0a3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
- Fixed #1290: Unecessary blank lines above nested imports when import comments turned on.
- Fixed #1297: Usage of `--add-imports` alongside `--check` is broken.
- Fixed #1289: Stream usage no longer auto picking up config file from current working directory.
- Fixed #1296: Force_single_line setting removes immediately following comment line.

### 5.0.4 July 6, 2020
- Fixed #1264: a regression with comment handling and `force_sort_within_sections` config option
Expand Down
17 changes: 6 additions & 11 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ def _with_from_imports(

while 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:
import_statement = wrap.line(
with_comments(
Expand Down Expand Up @@ -336,12 +342,6 @@ def _with_from_imports(
)
comments = None
else:
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)

while from_imports and from_imports[0] in as_imports:
from_import = from_imports.pop(0)
as_imports[from_import] = sorting.naturally(as_imports[from_import])
Expand Down Expand Up @@ -472,11 +472,6 @@ def _with_from_imports(
import_statement = wrap.line(import_statement, parsed.line_separator, config)

if import_statement:
above_comments = parsed.categorized_comments["above"]["from"].pop(module, None)
if above_comments: # pragma: no cover
if new_section_output and config.ensure_newline_before_comments:
new_section_output.append("")
new_section_output.extend(above_comments)
new_section_output.append(import_statement)
return new_section_output

Expand Down
15 changes: 15 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,18 @@ def on_email_deleted(self, email):
...
"""
assert isort.code(test_input) == test_input


def test_force_single_line_shouldnt_remove_preceding_comment_lines_issue_1296():
"""Tests to ensure force_single_line setting doesn't result in lost comments.
See: https://github.com/timothycrosley/isort/issues/1296
"""
test_input = """
# A comment
# A comment
# Oh no, I'm gone
from moo import foo
"""
# assert isort.code(test_input) == test_input
assert isort.code(test_input, force_single_line=True) == test_input

0 comments on commit 65e0a3f

Please sign in to comment.