From b27a5dcf039a72ff335fd81e7a313a51aa3b0593 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sun, 10 Jan 2021 23:19:03 -0800 Subject: [PATCH 1/7] Add test case for issue #1631 --- tests/unit/test_regressions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 89fa09274..a4d98cce3 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1515,3 +1515,13 @@ def test_isort_adding_second_comma_issue_1621(): ) """ ) + + +def test_isort_shouldnt_duplicate_comments_issue_1631(): + assert isort.check_code( + """ +import a # a comment +import a as b # b comment +""", + show_diff=True, + ) From cc69e6115174b1103b5b3c372e467e8cde0e229d Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 11 Jan 2021 22:06:08 -0800 Subject: [PATCH 2/7] Update output to take into account difference between as and non as import statements --- isort/parse.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index d93f559e2..f4844d506 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -410,9 +410,14 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte f"{top_level_module}.__combined_as__", [] ) else: - attach_comments_to = categorized_comments["straight"].setdefault( - module, [] - ) + if config.remove_redundant_aliases and as_name == module.split(".")[-1]: + attach_comments_to = categorized_comments["straight"].setdefault( + module, [] + ) + else: + attach_comments_to = categorized_comments["straight"].setdefault( + f"{module} as {as_name}" , [] + ) del just_imports[as_index : as_index + 2] if type_of_import == "from": From 433e3b0dcca0d6b9e05910f3c0430ddc46e38a1d Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 12 Jan 2021 22:08:40 -0800 Subject: [PATCH 3/7] Update output to respect as import comment location --- isort/output.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/isort/output.py b/isort/output.py index 1d51171d3..017ae4d4e 100644 --- a/isort/output.py +++ b/isort/output.py @@ -543,25 +543,25 @@ def _with_straight_imports( import_definition = [] if module in parsed.as_map["straight"]: if parsed.imports[section]["straight"][module]: - import_definition.append(f"{import_type} {module}") + import_definition.append((f"{import_type} {module}", module)) import_definition.extend( - f"{import_type} {module} as {as_import}" + (f"{import_type} {module} as {as_import}", f"{module} as {as_import}") for as_import in parsed.as_map["straight"][module] ) else: - import_definition.append(f"{import_type} {module}") + import_definition.append((f"{import_type} {module}", module)) comments_above = parsed.categorized_comments["above"]["straight"].pop(module, None) if comments_above: output.extend(comments_above) output.extend( with_comments( - parsed.categorized_comments["straight"].get(module), + parsed.categorized_comments["straight"].get(imodule), idef, removed=config.ignore_comments, comment_prefix=config.comment_prefix, ) - for idef in import_definition + for idef, imodule in import_definition ) return output From 804329d7776f3b97b8f248f44d314afb00ce753e Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 12 Jan 2021 22:09:02 -0800 Subject: [PATCH 4/7] Update test to use correct spacing for comments --- tests/unit/test_regressions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index a4d98cce3..3d586b2c8 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1520,8 +1520,8 @@ def test_isort_adding_second_comma_issue_1621(): def test_isort_shouldnt_duplicate_comments_issue_1631(): assert isort.check_code( """ -import a # a comment -import a as b # b comment +import a # a comment +import a as b # b comment """, show_diff=True, ) From c2133b3194e48ad7ba0730648bbbae62b12ec5bd Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 12 Jan 2021 22:21:14 -0800 Subject: [PATCH 5/7] Add test for duplicate alias case --- tests/unit/test_regressions.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 3d586b2c8..7ba1da770 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1525,3 +1525,15 @@ def test_isort_shouldnt_duplicate_comments_issue_1631(): """, show_diff=True, ) + assert ( + isort.code( + """ +import a # a comment +import a as a # b comment +""", + remove_redundant_aliases=True, + ) + == """ +import a # a comment; b comment +""" + ) From 836302fed1b6bc8720d4248529c989adcc41bbd5 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 12 Jan 2021 22:21:36 -0800 Subject: [PATCH 6/7] Clarify it effects straight imports only --- isort/parse.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index f4844d506..307015e74 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -410,13 +410,15 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte f"{top_level_module}.__combined_as__", [] ) else: - if config.remove_redundant_aliases and as_name == module.split(".")[-1]: + if type_of_import == "from" or ( + config.remove_redundant_aliases and as_name == module.split(".")[-1] + ): attach_comments_to = categorized_comments["straight"].setdefault( module, [] ) else: attach_comments_to = categorized_comments["straight"].setdefault( - f"{module} as {as_name}" , [] + f"{module} as {as_name}", [] ) del just_imports[as_index : as_index + 2] From 47882c5d3252227783d84ed029691fab0aa6d3cd Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 12 Jan 2021 22:52:52 -0800 Subject: [PATCH 7/7] Fix integration test --- tests/integration/test_projects_using_isort.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_projects_using_isort.py b/tests/integration/test_projects_using_isort.py index 2258cbfae..17cea29c4 100644 --- a/tests/integration/test_projects_using_isort.py +++ b/tests/integration/test_projects_using_isort.py @@ -61,7 +61,19 @@ def test_habitat_lab(tmpdir): def test_tmuxp(tmpdir): git_clone("https://github.com/tmux-python/tmuxp.git", tmpdir) - run_isort([str(tmpdir), "--skip", "cli.py", "--skip", "test_workspacebuilder.py"]) + run_isort( + [ + str(tmpdir), + "--skip", + "cli.py", + "--skip", + "test_workspacebuilder.py", + "--skip", + "test_cli.py", + "--skip", + "workspacebuilder.py", + ] + ) def test_websockets(tmpdir):