Skip to content

Commit

Permalink
Fix handling of mix indented imports
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Nov 4, 2020
1 parent 69f2a35 commit cd38767
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions isort/core.py
Expand Up @@ -276,7 +276,12 @@ def write(self, *a, **kw):
):
cimport_statement = True

if cimport_statement != cimports and import_section:
if cimport_statement != cimports or (
new_indent != indent
and import_section
and (not did_contain_imports or len(new_indent) < len(indent))
):
indent = new_indent
if import_section:
next_cimports = cimport_statement
next_import_section = import_statement
Expand All @@ -288,7 +293,7 @@ def write(self, *a, **kw):
else:
if new_indent != indent:
if import_section and did_contain_imports:
import_statement = import_statement.lstrip()
import_statement = indent + import_statement.lstrip()
else:
indent = new_indent
import_section += import_statement
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_ticketed_features.py
Expand Up @@ -892,5 +892,34 @@ def test_autofix_mixed_indent_imports_1575():
)
== """
import os
"""
)
assert (
isort.code(
"""
def one():
import os
import os
"""
)
== """
def one():
import os
import os
"""
)
assert (
isort.code(
"""
import os
import os
import os
import os
import os
"""
)
== """
import os
"""
)

0 comments on commit cd38767

Please sign in to comment.