Skip to content

Commit

Permalink
Fixed #1542: Bug in VERTICAL_PREFIX_FROM_MODULE_IMPORT wrap mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Oct 10, 2020
1 parent 245471d commit 8b9e45e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/

### 5.6.2 TBD
- 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.

### 5.6.1 [Hotfix] October 8, 2020
- Fixed #1546: Unstable (non-idempotent) behavior with certain src trees.
Expand Down
34 changes: 21 additions & 13 deletions isort/wrap_modes.py
Expand Up @@ -273,33 +273,41 @@ def vertical_hanging_indent_bracket(**interface):
def vertical_prefix_from_module_import(**interface):
if not interface["imports"]:
return ""

prefix_statement = interface["statement"]
interface["statement"] += interface["imports"].pop(0)
while interface["imports"]:
next_import = interface["imports"].pop(0)
next_statement = isort.comments.add_to_line(
interface["comments"],
interface["statement"] + ", " + next_import,
output_statement = prefix_statement + interface["imports"].pop(0)
comments = interface["comments"]

statement = output_statement
statement_with_comments = ""
for next_import in interface["imports"]:
statement = statement + ", " + next_import
statement_with_comments = isort.comments.add_to_line(
comments,
statement,
removed=interface["remove_comments"],
comment_prefix=interface["comment_prefix"],
)
if (
len(next_statement.split(interface["line_separator"])[-1]) + 1
len(statement_with_comments.split(interface["line_separator"])[-1]) + 1
> interface["line_length"]
):
next_statement = (
statement = (
isort.comments.add_to_line(
interface["comments"],
f"{interface['statement']}",
output_statement,
removed=interface["remove_comments"],
comment_prefix=interface["comment_prefix"],
)
+ f"{interface['line_separator']}{prefix_statement}{next_import}"
)
interface["comments"] = []
interface["statement"] = next_statement
return interface["statement"]

comments = []
output_statement = statement

if comments and statement_with_comments:
output_statement = statement_with_comments
return output_statement


@_wrap_mode
def hanging_indent_with_parentheses(**interface):
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_regressions.py
Expand Up @@ -1422,3 +1422,36 @@ def test_isort_shouldnt_split_skip_issue_1548():
import os
"""
)


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.
See: https://github.com/PyCQA/isort/issues/1542.
"""
assert isort.code(
"""
from xxxxxxxxxxxxxxxx import AAAAAAAAAA, BBBBBBBBBB
from xxxxxxxxxxxxxxxx import CCCCCCCCC, DDDDDDDDD # xxxxxxxxxxxxxxxxxx
print(CCCCCCCCC)
""",
multi_line_output=9,
) == """
from xxxxxxxxxxxxxxxx import AAAAAAAAAA, BBBBBBBBBB # xxxxxxxxxxxxxxxxxx
from xxxxxxxxxxxxxxxx import CCCCCCCCC, DDDDDDDDD
print(CCCCCCCCC)
"""

assert isort.check_code(
"""
from xxxxxxxxxxxxxxxx import AAAAAAAAAA, BBBBBBBBBB
from xxxxxxxxxxxxxxxx import CCCCCCCCC, DDDDDDDDD # xxxxxxxxxxxxxxxxxx isort: skip
print(CCCCCCCCC)
""",
show_diff = True,
multi_line_output=9,
)

0 comments on commit 8b9e45e

Please sign in to comment.