Skip to content

Commit

Permalink
Bugfix #1321: --combine-as loses comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sztamas committed Aug 12, 2020
1 parent 65f9e94 commit c9c1760
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
24 changes: 14 additions & 10 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ def _with_from_imports(
new_section_output.extend(above_comments)

if "*" in from_imports and config.combine_star:
if config.combine_as_imports:
comments = list(comments or ())
comments += parsed.categorized_comments["from"].pop(
f"{module}.__combined_as__", []
)
import_statement = wrap.line(
with_comments(
comments,
Expand Down Expand Up @@ -385,7 +390,6 @@ def _with_from_imports(
for as_import in as_imports[from_import]
)

star_import = False
if "*" in from_imports:
new_section_output.append(
with_comments(
Expand All @@ -396,7 +400,6 @@ def _with_from_imports(
)
)
from_imports.remove("*")
star_import = True
comments = None

for from_import in copy.copy(from_imports):
Expand Down Expand Up @@ -428,15 +431,16 @@ def _with_from_imports(
)
):
from_import_section.append(from_imports.pop(0))
if star_import:
import_statement = import_start + (", ").join(from_import_section)
else:
import_statement = with_comments(
comments,
import_start + (", ").join(from_import_section),
removed=config.ignore_comments,
comment_prefix=config.comment_prefix,
if config.combine_as_imports:
comments = parsed.categorized_comments["from"].pop(
f"{module}.__combined_as__", ()
)
import_statement = with_comments(
comments,
import_start + (", ").join(from_import_section),
removed=config.ignore_comments,
comment_prefix=config.comment_prefix,
)
if not from_import_section:
import_statement = ""

Expand Down
12 changes: 10 additions & 2 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,12 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
if "as" in just_imports and (just_imports.index("as") + 1) < len(just_imports):
straight_import = False
while "as" in just_imports:
nested_module = None
as_index = just_imports.index("as")
if type_of_import == "from":
nested_module = just_imports[as_index - 1]
module = just_imports[0] + "." + nested_module
top_level_module = just_imports[0]
module = top_level_module + "." + nested_module
as_name = just_imports[as_index + 1]
if nested_module == as_name and config.remove_redundant_aliases:
pass
Expand All @@ -336,7 +338,13 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
pass
elif as_name not in as_map["straight"][module]:
as_map["straight"][module].append(as_name)
if not config.combine_as_imports:

if config.combine_as_imports and nested_module:
categorized_comments["from"].setdefault(
f"{top_level_module}.__combined_as__", []
).extend(comments)
comments = []
else:
categorized_comments["straight"][module] = comments
comments = []
del just_imports[as_index : as_index + 2]
Expand Down
28 changes: 28 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,31 @@ def test_windows_diff_too_large_misrepresentative_issue_1348(test_path):
assert diff_output.read().endswith(
"-1,5 +1,5 @@\n+import a\r\n import b\r\n" "-import a\r\n \r\n \r\n def func():\r\n"
)


def test_combine_as_does_not_lose_comments_issue_1321():
"""Test to ensure isort doesn't lose comments when --combine-as is used.
See: https://github.com/timothycrosley/isort/issues/1321
"""
test_input = """
from foo import * # noqa
from foo import bar as quux # other
from foo import x as a # noqa
import operator as op # op comment
import datetime as dtime # dtime comment
from datetime import date as d # dcomm
from datetime import datetime as dt # dtcomm
"""

expected_output = """
import datetime as dtime # dtime comment
import operator as op # op comment
from datetime import date as d, datetime as dt # dcomm; dtcomm
from foo import * # noqa
from foo import bar as quux, x as a # other; noqa
"""

assert isort.code(test_input, combine_as_imports=True) == expected_output

0 comments on commit c9c1760

Please sign in to comment.