summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2020-08-11 21:41:48 -0700
committerGitHub <noreply@github.com>2020-08-11 21:41:48 -0700
commit45fdfe561ac60efbb7b742431f23c16dd713e4a3 (patch)
tree3604092a04489dd256973a9ae69943c60b545c2a
parent85e07d44cc49ebbe605c24eefc6779968db3d684 (diff)
parentdf8fdc5c1b3c07e01c70e42ecd8a49e3552dada1 (diff)
downloadisort-45fdfe561ac60efbb7b742431f23c16dd713e4a3.tar.gz
Merge pull request #1373 from sztamas/issue/1232/length_sort_straight
Adds length_sort_straight option
-rw-r--r--docs/configuration/options.md13
-rw-r--r--isort/main.py7
-rw-r--r--isort/output.py5
-rw-r--r--isort/settings.py1
-rw-r--r--isort/sorting.py7
-rw-r--r--tests/test_isort.py23
6 files changed, 54 insertions, 2 deletions
diff --git a/docs/configuration/options.md b/docs/configuration/options.md
index 01ac6128..dee1a860 100644
--- a/docs/configuration/options.md
+++ b/docs/configuration/options.md
@@ -293,6 +293,19 @@ Sort imports by their string length.
- --ls
- --length-sort
+## Length Sort Straight Imports
+
+Sort straight imports by their string length. Similar to `length_sort` but applies only to
+straight imports and doesn't affect from imports.
+
+**Type:** Bool
+**Default:** `False`
+**Python & Config File Name:** length_sort_straight
+**CLI Flags:**
+
+- --lss
+- --length-sort-straight
+
## Length Sort Sections
**No Description**
diff --git a/isort/main.py b/isort/main.py
index 39c96b9f..b591edb0 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -328,6 +328,13 @@ def _build_arg_parser() -> argparse.ArgumentParser:
action="store_true",
)
parser.add_argument(
+ "--lss",
+ "--length-sort-straight",
+ help="Sort straight imports by their string length.",
+ dest="length_sort_straight",
+ action="store_true",
+ )
+ parser.add_argument(
"-m",
"--multi-line",
dest="multi_line_output",
diff --git a/isort/output.py b/isort/output.py
index d745f9a4..33321b74 100644
--- a/isort/output.py
+++ b/isort/output.py
@@ -51,7 +51,10 @@ def sorted_imports(
for section in sections:
straight_modules = parsed.imports[section]["straight"]
straight_modules = sorting.naturally(
- straight_modules, key=lambda key: sorting.module_key(key, config, section_name=section)
+ straight_modules,
+ key=lambda key: sorting.module_key(
+ key, config, section_name=section, straight_import=True
+ ),
)
from_modules = parsed.imports[section]["from"]
from_modules = sorting.naturally(
diff --git a/isort/settings.py b/isort/settings.py
index 68f09bb6..304e39ca 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -132,6 +132,7 @@ class _Config:
indent: str = " " * 4
comment_prefix: str = " #"
length_sort: bool = False
+ length_sort_straight: bool = False
length_sort_sections: FrozenSet[str] = frozenset()
add_imports: FrozenSet[str] = frozenset()
remove_imports: FrozenSet[str] = frozenset()
diff --git a/isort/sorting.py b/isort/sorting.py
index 4d010a11..1664a2f2 100644
--- a/isort/sorting.py
+++ b/isort/sorting.py
@@ -13,6 +13,7 @@ def module_key(
sub_imports: bool = False,
ignore_case: bool = False,
section_name: Optional[Any] = None,
+ straight_import: Optional[bool] = False,
) -> str:
match = re.match(r"^(\.+)\s*(.*)", module_name)
if match:
@@ -41,7 +42,11 @@ def module_key(
if not config.case_sensitive:
module_name = module_name.lower()
- length_sort = config.length_sort or str(section_name).lower() in config.length_sort_sections
+ length_sort = (
+ config.length_sort
+ or (config.length_sort_straight and straight_import)
+ or str(section_name).lower() in config.length_sort_sections
+ )
_length_sort_maybe = length_sort and (str(len(module_name)) + ":" + module_name) or module_name
return f"{module_name in config.force_to_top and 'A' or 'B'}{prefix}{_length_sort_maybe}"
diff --git a/tests/test_isort.py b/tests/test_isort.py
index 0f7d7136..2ad132fd 100644
--- a/tests/test_isort.py
+++ b/tests/test_isort.py
@@ -605,11 +605,33 @@ def test_length_sort() -> None:
)
+def test_length_sort_straight() -> None:
+ """Test setting isort to sort straight imports on length instead of alphabetically."""
+ test_input = (
+ "import medium_sizeeeeeeeeeeeeee\n"
+ "import shortie\n"
+ "import looooooooooooooooooooooooooooooooooooooong\n"
+ "from medium_sizeeeeeeeeeeeeee import b\n"
+ "from shortie import c\n"
+ "from looooooooooooooooooooooooooooooooooooooong import a\n"
+ )
+ test_output = isort.code(test_input, length_sort_straight=True)
+ assert test_output == (
+ "import shortie\n"
+ "import medium_sizeeeeeeeeeeeeee\n"
+ "import looooooooooooooooooooooooooooooooooooooong\n"
+ "from looooooooooooooooooooooooooooooooooooooong import a\n"
+ "from medium_sizeeeeeeeeeeeeee import b\n"
+ "from shortie import c\n"
+ )
+
+
def test_length_sort_section() -> None:
"""Test setting isort to sort on length instead of alphabetically for a specific section."""
test_input = (
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
+ "import datetime\n"
"import sys\n"
"import os\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
@@ -619,6 +641,7 @@ def test_length_sort_section() -> None:
assert test_output == (
"import os\n"
"import sys\n"
+ "import datetime\n"
"\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"import medium_sizeeeeeeeeeeeeea\n"