Skip to content

Commit

Permalink
Fix issue #1523: isort should keep all as and non as imports
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Oct 5, 2020
1 parent 0971e63 commit bacf2fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
13 changes: 8 additions & 5 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit bacf2fd

Please sign in to comment.