Skip to content

Commit

Permalink
Fixed #1290: Unecessary blank lines above nested imports when import …
Browse files Browse the repository at this point in the history
…comments turned on.
  • Loading branch information
timothycrosley committed Jul 8, 2020
1 parent bff46f3 commit a35e464
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
- Fixed #1284: Regression when sorting `.pyi` files from CLI using black profile.
- Fixed #1275 & #1283: Blank line after docstring removed.
- Fixed #1298: CLI Help out of date with isort 5.
- Fixed #1290: Unecessary blank lines above nested imports when import comments turned on.

### 5.0.4 July 6, 2020
- Fixed #1264: a regression with comment handling and `force_sort_within_sections` config option
Expand Down
49 changes: 25 additions & 24 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,31 +560,32 @@ def _sort_imports(
extension,
import_type="cimport" if cimports else "import",
)
if indent:
sorted_import_section = (
leading_whitespace
+ textwrap.indent(sorted_import_section, indent).strip()
+ trailing_whitespace
)

if not made_changes:
if config.ignore_whitespace:
compare_in = remove_whitespace(
raw_import_section, line_separator=line_separator
).strip()
compare_out = remove_whitespace(
sorted_import_section, line_separator=line_separator
).strip()
else:
compare_in = raw_import_section.strip()
compare_out = sorted_import_section.strip()

if compare_out != compare_in:
made_changes = True
if not (import_section.strip() and not sorted_import_section):
if indent:
sorted_import_section = (
leading_whitespace
+ textwrap.indent(sorted_import_section, indent).strip()
+ trailing_whitespace
)

output_stream.write(sorted_import_section)
if not line and not indent and next_import_section:
output_stream.write(line_separator)
if not made_changes:
if config.ignore_whitespace:
compare_in = remove_whitespace(
raw_import_section, line_separator=line_separator
).strip()
compare_out = remove_whitespace(
sorted_import_section, line_separator=line_separator
).strip()
else:
compare_in = raw_import_section.strip()
compare_out = sorted_import_section.strip()

if compare_out != compare_in:
made_changes = True

output_stream.write(sorted_import_section)
if not line and not indent and next_import_section:
output_stream.write(line_separator)

if indent:
output_stream.write(line)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_blank_lined_removed_issue_1275():
'''
)


def test_blank_lined_removed_issue_1283():
"""Ensure isort doesn't accidentally remove blank lines after __version__ identifiers.
See: https://github.com/timothycrosley/isort/issues/1283
Expand All @@ -70,3 +71,35 @@ def test_blank_lined_removed_issue_1283():
from starlette import status
"""
assert isort.code(test_input) == test_input


def test_extra_blank_line_added_nested_imports_issue_1290():
"""Ensure isort doesn't added unecessary blank lines above nested imports.
See: https://github.com/timothycrosley/isort/issues/1290
"""
test_input = '''from typing import TYPE_CHECKING
# Special imports
from special import thing
if TYPE_CHECKING:
# Special imports
from special import another_thing
def func():
"""Docstring"""
# Special imports
from special import something_else
return
'''
assert (
isort.code(
test_input,
import_heading_special="Special imports",
known_special=["special"],
sections=["FUTURE", "STDLIB", "THIRDPARTY", "SPECIAL", "FIRSTPARTY", "LOCALFOLDER"],
)
== test_input
)

0 comments on commit a35e464

Please sign in to comment.