From bacf2fd56c807e8a62305c2d15fe11e402e840f0 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sun, 4 Oct 2020 23:20:11 -0700 Subject: [PATCH] Fix issue #1523: isort should keep all as and non as imports --- isort/parse.py | 13 ++++++++----- tests/unit/test_isort.py | 23 +++++++++++++++++++++++ tests/unit/test_regressions.py | 5 ++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index d7e22a2d8..9a80e97b9 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -338,8 +338,10 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte ] attach_comments_to: Optional[List[Any]] = None + direct_imports = just_imports[1:] + straight_import = True if "as" in just_imports and (just_imports.index("as") + 1) < len(just_imports): - straight_imports = set() + straight_import = False while "as" in just_imports: nested_module = None as_index = just_imports.index("as") @@ -348,6 +350,9 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte top_level_module = just_imports[0] module = top_level_module + "." + nested_module as_name = just_imports[as_index + 1] + direct_imports.remove(nested_module) + direct_imports.remove(as_name) + direct_imports.remove("as") if nested_module == as_name and config.remove_redundant_aliases: pass elif as_name not in as_map["from"][module]: @@ -379,8 +384,6 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte module, [] ) del just_imports[as_index : as_index + 2] - else: - straight_imports = set(just_imports[1:]) if type_of_import == "from": import_from = just_imports.pop(0) @@ -435,11 +438,11 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte if import_from not in root: root[import_from] = OrderedDict( - (module, straight_import) for module in just_imports + (module, module in direct_imports) for module in just_imports ) else: root[import_from].update( - (module, straight_import | root[import_from].get(module, False)) + (module, root[import_from].get(module, False) or module in direct_imports) for module in just_imports ) diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 4598de8cd..c07d655d7 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -966,6 +966,29 @@ def test_check_newline_in_imports(capsys) -> None: out, _ = capsys.readouterr() assert "SUCCESS" in out + # if the verbose is only on modified outputs no output will be given + assert api.check_code_string( + code=test_input, + multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, + line_length=20, + verbose=True, + only_modified=True, + ) + out, _ = capsys.readouterr() + assert not out + + # we can make the input invalid to again see output + test_input = "from lib1 import (\n sub2,\n sub1,\n sub3\n)\n" + assert not api.check_code_string( + code=test_input, + multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, + line_length=20, + verbose=True, + only_modified=True, + ) + out, _ = capsys.readouterr() + assert out + def test_forced_separate() -> None: """Ensure that forcing certain sub modules to show separately works as expected.""" diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 3a1d94e06..f334e4827 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1283,4 +1283,7 @@ def test_isort_should_keep_all_as_and_non_as_imports_issue_1523(): assert isort.check_code( """ from selenium.webdriver import Remote, Remote as Driver -""", show_diff=True, combine_as_imports=True) +""", + show_diff=True, + combine_as_imports=True, + )