Skip to content

Commit

Permalink
Merge pull request #1557 from PyCQA/issue/1556/remove-uneeded-new-line
Browse files Browse the repository at this point in the history
Issue/1556/remove uneeded new line
  • Loading branch information
timothycrosley committed Oct 13, 2020
2 parents 491b346 + b487874 commit a6b253e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
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.4 October TBD, 2020
- Fixed #1556: Empty line added between imports that should be skipped.

### 5.6.3 October 11, 2020
- Improved packaging of test files alongside source distribution (see: https://github.com/PyCQA/isort/pull/1555).

Expand Down
35 changes: 27 additions & 8 deletions isort/parse.py
Expand Up @@ -218,17 +218,36 @@ 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
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 a6b253e

Please sign in to comment.