Skip to content

Commit

Permalink
Fixed #1556: Empty line added between imports that should be skipped.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Oct 13, 2020
1 parent c241234 commit 49bb9ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
NOTE: isort follows the [semver](https://semver.org/) versioning standard.
Find out more about isort's release policy [here](https://pycqa.github.io/isort/docs/major_releases/release_policy/).

### 5.6.3 October TBD, 2020
- Fixed #1556: Empty line added between imports that should be skipped.

### 5.6.2 October 10, 2020
- Fixed #1548: On rare occasions an unecessary empty line can be added when an import is marked as skipped.
- Fixed #1542: Bug in VERTICAL_PREFIX_FROM_MODULE_IMPORT wrap mode.
Expand Down
34 changes: 26 additions & 8 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,35 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
import_index = index - 1
while import_index and not in_lines[import_index - 1]:
import_index -= 1
elif "isort:skip" in line or "isort: skip" in line:
commentless = line.split("#", 1)[0]
else:
commentless = line.split("#", 1)[0].strip()
if (
"(" in commentless
and not commentless.rstrip().endswith(")")
and import_index < line_count
("isort:skip" in line
or "isort: skip" in line)
and "(" in commentless
and ")" not in commentless
):
import_index = index
while import_index < line_count and not commentless.rstrip().endswith(")"):
commentless = in_lines[import_index].split("#", 1)[0]
import_index += 1

starting_line = line
while "isort:skip" in starting_line or "isort: skip" in starting_line:
commentless = starting_line.split("#", 1)[0]
if (
"(" in commentless
and not commentless.rstrip().endswith(")")
and import_index < line_count
):

while import_index < line_count and not commentless.rstrip().endswith(")"):
commentless = in_lines[import_index].split("#", 1)[0]
import_index += 1
else:
import_index += 1

if import_index >= line_count:
break
else:
starting_line = in_lines[import_index]

line, *end_of_line_comment = line.split("#", 1)
if ";" in line:
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,33 @@ def test_isort_shouldnt_split_skip_issue_1548():
)


def test_isort_shouldnt_split_skip_issue_1556():
assert isort.check_code(
"""
from tools.dependency_pruning.prune_dependencies import ( # isort:skip
prune_dependencies,
)
from tools.developer_pruning.prune_developers import ( # isort:skip
prune_developers,
)
""",
show_diff=True,
profile="black",
float_to_top=True,
)
assert isort.check_code(
"""
from tools.dependency_pruning.prune_dependencies import ( # isort:skip
prune_dependencies,
)
from tools.developer_pruning.prune_developers import x # isort:skip
""",
show_diff=True,
profile="black",
float_to_top=True,
)


def test_isort_losing_imports_vertical_prefix_from_module_import_wrap_mode_issue_1542():
"""Ensure isort doesnt lose imports when a comment is combined with an import and
wrap mode VERTICAL_PREFIX_FROM_MODULE_IMPORT is used.
Expand Down

0 comments on commit 49bb9ba

Please sign in to comment.