Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,12 @@ def process(
):
cimport_statement = True

if cimport_statement != cimports or (
if cimport_statement != cimports:
indent = new_indent
if (
new_indent != indent
and import_section
and (not did_contain_imports or len(new_indent) < len(indent))
and (not did_contain_imports and len(new_indent) < len(indent))
):
indent = new_indent
if import_section:
Expand Down Expand Up @@ -421,7 +423,7 @@ def process(

if indent:
import_section = "".join(
line[len(indent) :] for line in import_section.splitlines(keepends=True)
line[len(line) - len(line.lstrip()) :] for line in import_section.splitlines(keepends=True)
)

parsed_content = parse.file_contents(import_section, config=config)
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5689,6 +5689,22 @@ def test_reexport_not_last_line() -> None:
assert isort.code(test_input, config=Config(sort_reexports=True)) == expd_output


def test_strip_leading_characters_with_odd_indentation() -> None:
""" Test to ensure that isort doesn't strip away leading character
with odd indentation
"""
test_input = """
#!/usr/bin/env python3

if True:
#this comment breaks
# this is another comment
import sys
"""

assert isort.code(test_input) == test_input


def test_reexport_multiline_import() -> None:
test_input = """from m import (
bar,
Expand Down
Loading