summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2021-03-15 23:04:57 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2021-03-15 23:04:57 -0700
commit9c18cbb630749925b71f9904e4581a8657c33d3a (patch)
tree8702cab734ff7f150873a346a6e455854dafb8d8
parent2bce0f2c197a6c7b70c7ae2a6c9c0cbded2a6600 (diff)
downloadisort-9c18cbb630749925b71f9904e4581a8657c33d3a.tar.gz
Fix issue #1688: from statements removed when invalid
-rw-r--r--isort/core.py67
-rw-r--r--isort/parse.py8
2 files changed, 45 insertions, 30 deletions
diff --git a/isort/core.py b/isort/core.py
index 59878998..d763ad46 100644
--- a/isort/core.py
+++ b/isort/core.py
@@ -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]
@@ -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
diff --git a/isort/parse.py b/isort/parse.py
index 307015e7..714e39ab 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -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
@@ -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])
@@ -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])
@@ -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 (
@@ -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