Skip to content

Commit

Permalink
Fix issue #1688: from statements removed when invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 16, 2021
1 parent 2bce0f2 commit 9c18cbb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
67 changes: 37 additions & 30 deletions isort/core.py
Expand Up @@ -246,9 +246,6 @@ def process(
):
import_section += line
elif stripped_line.startswith(IMPORT_START_IDENTIFIERS):
did_contain_imports = contains_imports
contains_imports = True

new_indent = line[: -len(line.lstrip())]
import_statement = line
stripped_line = line.strip().split("#")[0]
Expand All @@ -266,37 +263,47 @@ def process(
stripped_line = line.strip().split("#")[0]
import_statement += line

cimport_statement: bool = False
if (
import_statement.lstrip().startswith(CIMPORT_IDENTIFIERS)
or " cimport " in import_statement
or " cimport*" in import_statement
or " cimport(" in import_statement
or ".cimport" in import_statement
):
cimport_statement = True

if cimport_statement != cimports or (
new_indent != indent
and import_section
and (not did_contain_imports or len(new_indent) < len(indent))
import_statement.lstrip().startswith("from")
and "import" not in import_statement
):
indent = new_indent
if import_section:
next_cimports = cimport_statement
next_import_section = import_statement
import_statement = ""
not_imports = True
line = ""
else:
cimports = cimport_statement
line = import_statement
not_imports = True
else:
if new_indent != indent:
if import_section and did_contain_imports:
import_statement = indent + import_statement.lstrip()
did_contain_imports = contains_imports
contains_imports = True

cimport_statement: bool = False
if (
import_statement.lstrip().startswith(CIMPORT_IDENTIFIERS)
or " cimport " in import_statement
or " cimport*" in import_statement
or " cimport(" in import_statement
or ".cimport" in import_statement
):
cimport_statement = True

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
import_statement = ""
not_imports = True
line = ""
else:
indent = new_indent
import_section += import_statement
cimports = cimport_statement
else:
if new_indent != indent:
if import_section and did_contain_imports:
import_statement = indent + import_statement.lstrip()
else:
indent = new_indent
import_section += import_statement
else:
not_imports = True

Expand Down
8 changes: 8 additions & 0 deletions isort/parse.py
Expand Up @@ -260,6 +260,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
for statement in statements:
line, raw_line = _normalize_line(statement)
type_of_import = import_type(line, config) or ""
raw_lines = [raw_line]
if not type_of_import:
out_lines.append(raw_line)
continue
Expand Down Expand Up @@ -288,6 +289,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
):
nested_comments[stripped_line] = comments[-1]
import_string += line_separator + line
raw_lines.append(line)
else:
while line.strip().endswith("\\"):
line, new_comment = parse_comments(in_lines[index])
Expand All @@ -310,6 +312,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
):
nested_comments[stripped_line] = comments[-1]
import_string += line_separator + line
raw_lines.append(line)

while not line.split("#")[0].strip().endswith(")") and index < line_count:
line, new_comment = parse_comments(in_lines[index])
Expand All @@ -325,6 +328,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
):
nested_comments[stripped_line] = comments[-1]
import_string += line_separator + line
raw_lines.append(line)

stripped_line = _strip_syntax(line).strip()
if (
Expand All @@ -348,6 +352,10 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
.replace("\\", " ")
.replace("\n", " ")
)
if "import " not in import_string:
out_lines.extend(raw_lines)
continue

if " cimport " in import_string:
parts = import_string.split(" cimport ")
cimports = True
Expand Down

0 comments on commit 9c18cbb

Please sign in to comment.