diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2020-10-04 23:50:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-04 23:50:10 -0700 |
commit | 10995255403a8990177283e165f32541452d3c3e (patch) | |
tree | 805d3c8c3b48b044b0844426a838aa43a720db61 | |
parent | 66bc8642a8dc5aeeafcf8899b3b40dbec4062bd6 (diff) | |
parent | 8bad5b06e5bb1b5eabe8858ddc8622e48ed4db5d (diff) | |
download | isort-10995255403a8990177283e165f32541452d3c3e.tar.gz |
Merge pull request #1529 from PyCQA/issue/1523/isort-removes-non-as-imports-on-same-line
Issue/1523/isort removes non as imports on same line
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | isort/parse.py | 11 | ||||
-rw-r--r-- | tests/unit/test_isort.py | 23 | ||||
-rw-r--r-- | tests/unit/test_regressions.py | 14 |
4 files changed, 48 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a33d78fc..9734f518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,9 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ - Fixed #1492: --check does not work with stdin source. - Fixed #1499: isort gets confused by single line, multi-line style comments when using float-to-top. - Fixed #1525: Some warnings can't be disabled with --quiet. -Potentially breaking changes: + - Fixed #1523: in rare cases isort can ignore direct from import if as import is also on same line. + + Potentially breaking changes: - Fixed #1486: "Google" profile is not quite Google style. - Fixed "PyCharm" profile to always add 2 lines to be consistent with what PyCharm "Optimize Imports" does. diff --git a/isort/parse.py b/isort/parse.py index 9348267c..9a80e97b 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -336,8 +336,10 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte item.replace("{|", "{ ").replace("|}", " }") for item in _strip_syntax(import_string).split() ] - straight_import = True + 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_import = False while "as" in just_imports: @@ -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]: @@ -433,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 4598de8c..c07d655d 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 75759522..f334e482 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1273,3 +1273,17 @@ def d(): ''', show_diff=True, ) + + +def test_isort_should_keep_all_as_and_non_as_imports_issue_1523(): + """isort should keep as and non-as imports of the same path that happen to exist within the + same statement. + See: https://github.com/PyCQA/isort/issues/1523. + """ + assert isort.check_code( + """ +from selenium.webdriver import Remote, Remote as Driver +""", + show_diff=True, + combine_as_imports=True, + ) |