summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-01-05 00:37:29 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2020-01-05 00:37:29 -0800
commit8c8330dcaa7a4b38bcd2bd51f7eced890e049beb (patch)
tree6da9f9219e7727aba0122e06f523fce9a9c60402
parent7dab76f00370c97ee68d9aaed10781cb91d4f933 (diff)
downloadisort-8c8330dcaa7a4b38bcd2bd51f7eced890e049beb.tar.gz
Initially working cimport support
-rw-r--r--isort/api.py17
-rw-r--r--isort/output.py21
-rw-r--r--isort/parse.py22
-rw-r--r--tests/test_comments.py1
-rw-r--r--tests/test_output.py1
-rw-r--r--tests/test_parse.py1
-rw-r--r--tests/test_wrap_modes.py1
7 files changed, 49 insertions, 15 deletions
diff --git a/isort/api.py b/isort/api.py
index b7126999..99d717eb 100644
--- a/isort/api.py
+++ b/isort/api.py
@@ -17,8 +17,6 @@ from .format import format_natural, remove_whitespace, show_unified_diff
from .io import File
from .settings import DEFAULT_CONFIG, FILE_SKIP_COMMENTS, Config
-IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*")
-
CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport")
IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS
COMMENT_INDICATORS = ('"""', "'''", "'", '"', "#")
@@ -249,9 +247,10 @@ def sort_imports(
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
+ or " cimport*" in import_statement
+ or " cimport(" in import_statement
+ or ".cimport" in import_statement
):
cimport_statement = True
@@ -264,7 +263,7 @@ def sort_imports(
else:
cimports = cimport_statement
- import_section + import_statement
+ import_section += import_statement
else:
not_imports = True
@@ -280,6 +279,10 @@ def sort_imports(
contains_imports = True
add_imports = []
+ if next_import_section and not import_section:
+ import_section = next_import_section
+ next_import_section = ""
+
if import_section:
if add_imports and not indent:
import_section += line_separator.join(add_imports) + line_separator
@@ -313,6 +316,8 @@ def sort_imports(
)
output_stream.write(sorted_import_section)
+ if not line and next_import_section:
+ output_stream.write(line_separator)
if indent:
output_stream.write(line)
diff --git a/isort/output.py b/isort/output.py
index 73b1e4cb..5f2659f7 100644
--- a/isort/output.py
+++ b/isort/output.py
@@ -11,7 +11,10 @@ from .settings import DEFAULT_CONFIG, Config
def sorted_imports(
- parsed: parse.ParsedContent, config: Config = DEFAULT_CONFIG, extension: str = "py", import_type: str="import"
+ parsed: parse.ParsedContent,
+ config: Config = DEFAULT_CONFIG,
+ extension: str = "py",
+ import_type: str = "import",
) -> str:
"""Adds the imports back to the file.
@@ -66,11 +69,23 @@ def sorted_imports(
if config.lines_between_types and from_modules and straight_modules:
section_output.extend([""] * config.lines_between_types)
section_output = _with_straight_imports(
- parsed, config, straight_modules, section, section_output, remove_imports, import_type
+ parsed,
+ config,
+ straight_modules,
+ section,
+ section_output,
+ remove_imports,
+ import_type,
)
else:
section_output = _with_straight_imports(
- parsed, config, straight_modules, section, section_output, remove_imports, import_type
+ parsed,
+ config,
+ straight_modules,
+ section,
+ section_output,
+ remove_imports,
+ import_type,
)
if config.lines_between_types and from_modules and straight_modules:
section_output.extend([""] * config.lines_between_types)
diff --git a/isort/parse.py b/isort/parse.py
index 3bfa3b0f..6810176e 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -65,6 +65,7 @@ def import_type(line: str) -> Optional[str]:
def _strip_syntax(import_string: str) -> str:
import_string = import_string.replace("_import", "[[i]]")
+ import_string = import_string.replace("_cimport", "[[ci]]")
for remove_syntax in ["\\", "(", ")", ","]:
import_string = import_string.replace(remove_syntax, " ")
import_list = import_string.split()
@@ -73,6 +74,7 @@ def _strip_syntax(import_string: str) -> str:
import_list.remove(key)
import_string = " ".join(import_list)
import_string = import_string.replace("[[i]]", "_import")
+ import_string = import_string.replace("[[ci]]", "_cimport")
return import_string.replace("{ ", "{|").replace(" }", "|}")
@@ -108,7 +110,11 @@ def skip_line(
if ";" in line:
for part in (part.strip() for part in line.split(";")):
- if part and not part.startswith("from ") and not part.startswith(("import ", "cimport ")):
+ if (
+ part
+ and not part.startswith("from ")
+ and not part.startswith(("import ", "cimport "))
+ ):
skip_line = True
return (bool(skip_line or in_quote), in_quote)
@@ -267,22 +273,26 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
and new_comment
):
nested_comments[stripped_line] = comments[-1]
- if import_string.strip().endswith((" import", " cimport")) or line.strip().startswith(("import ", "cimport ")):
+ if import_string.strip().endswith(
+ (" import", " cimport")
+ ) or line.strip().startswith(("import ", "cimport ")):
import_string += line_separator + line
else:
import_string = import_string.rstrip().rstrip("\\") + " " + line.lstrip()
if type_of_import == "from":
- cimport: bool
+ cimports: bool
import_string = import_string.replace("import(", "import (")
if " cimport " in import_string:
parts = import_string.split(" cimport ")
- cimport = True
+ cimports = True
+
else:
parts = import_string.split(" import ")
- cimport = False
+ cimports = False
+
from_import = parts[0].split(" ")
- import_string = " cimport " if cimport else " import ".join(
+ import_string = (" cimport " if cimports else " import ").join(
[from_import[0] + " " + "".join(from_import[1:])] + parts[1:]
)
diff --git a/tests/test_comments.py b/tests/test_comments.py
index 1805f137..b1b4ed7b 100644
--- a/tests/test_comments.py
+++ b/tests/test_comments.py
@@ -1,4 +1,5 @@
from hypothesis_auto import auto_pytest_magic
+
from isort import comments
auto_pytest_magic(comments.parse)
diff --git a/tests/test_output.py b/tests/test_output.py
index f9e04aec..e42093b5 100644
--- a/tests/test_output.py
+++ b/tests/test_output.py
@@ -1,6 +1,7 @@
import sys
from hypothesis_auto import auto_pytest_magic
+
from isort import output
auto_pytest_magic(output.with_comments, auto_allow_exceptions_=(ValueError,))
diff --git a/tests/test_parse.py b/tests/test_parse.py
index f5846bd7..efc3a22a 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -1,4 +1,5 @@
from hypothesis_auto import auto_pytest_magic
+
from isort import parse
from isort.settings import Config
diff --git a/tests/test_wrap_modes.py b/tests/test_wrap_modes.py
index 6c8a6655..dee26d4f 100644
--- a/tests/test_wrap_modes.py
+++ b/tests/test_wrap_modes.py
@@ -1,6 +1,7 @@
import sys
from hypothesis_auto import auto_pytest_magic
+
from isort import wrap_modes
auto_pytest_magic(wrap_modes.grid, auto_allow_exceptions_=(ValueError,))