From c9c176082cc8df37b8187c444ad85942704fad9c Mon Sep 17 00:00:00 2001 From: Tamas Szabo Date: Wed, 12 Aug 2020 18:11:26 +0300 Subject: Bugfix #1321: --combine-as loses comment --- isort/output.py | 24 ++++++++++++++---------- isort/parse.py | 12 ++++++++++-- tests/test_regressions.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/isort/output.py b/isort/output.py index 33321b74..086ece72 100644 --- a/isort/output.py +++ b/isort/output.py @@ -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, @@ -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( @@ -396,7 +400,6 @@ def _with_from_imports( ) ) from_imports.remove("*") - star_import = True comments = None for from_import in copy.copy(from_imports): @@ -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 = "" diff --git a/isort/parse.py b/isort/parse.py index 350188a7..2ab43acf 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -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 @@ -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] diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 5d1d0413..8236329b 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -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 -- cgit v1.2.1