Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue when non-indented lines get stripped with isort #2109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions isort/core.py
Expand Up @@ -332,10 +332,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 @@ -415,7 +417,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
Expand Up @@ -5671,3 +5671,19 @@ def test_reexport_not_last_line() -> None:
meme = "rickroll"
"""
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