summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-07-17 20:37:42 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2020-07-17 20:37:42 -0700
commit6988259a3d0dcf7d127c1f629a6874a7400adc52 (patch)
treea3a9b2729cc4b78aae222f49c4a583fbcb8131a5
parent9b0fd43956ca9881a1fec9dddb16b4eb18923a7a (diff)
downloadisort-issue/1321/combine_as_comments.tar.gz
Save progress on fixing 1321issue/1321/combine_as_comments
-rw-r--r--isort/output.py1
-rw-r--r--isort/parse.py5
-rw-r--r--tests/test_regressions.py14
3 files changed, 18 insertions, 2 deletions
diff --git a/isort/output.py b/isort/output.py
index 620a4159..5098fd9c 100644
--- a/isort/output.py
+++ b/isort/output.py
@@ -413,6 +413,7 @@ def _with_from_imports(
and parsed.imports[section]["from"][module][from_import]
)
):
+ #breakpoint()
from_import_section.append(from_imports.pop(0))
if star_import:
import_statement = import_start + (", ").join(from_import_section)
diff --git a/isort/parse.py b/isort/parse.py
index 283d09a7..a1a70e2e 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -322,6 +322,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
nested_module = just_imports[as_index - 1]
module = just_imports[0] + "." + nested_module
as_name = just_imports[as_index + 1]
+ categorized_comments["nested"].setdefault(just_imports[0], {})[nested_module] = ", ".join(comments)
if nested_module == as_name and config.remove_redundant_aliases:
pass
elif as_name not in as_map["from"][module]:
@@ -333,9 +334,9 @@ 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:
+
categorized_comments["straight"][module] = comments
- comments = []
+ comments = []
del just_imports[as_index : as_index + 2]
if type_of_import == "from":
import_from = just_imports.pop(0)
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 275c8b20..721621fa 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -427,3 +427,17 @@ import logging
from ast import excepthandler
"""
)
+
+
+def test_losing_comments_when_combine_as_is_set_issue_1321():
+ """Tests to ensure isort doesn't remove comments information when used in conjunction with
+ combine_as_imports.
+ See: https://github.com/timothycrosley/isort/issues/1321
+ """
+ assert isort.check_code(
+ """from foo import * # noqa
+from foo import bar as quux # noqa
+""",
+ show_diff=True,
+ combine_as_imports=True,
+ )