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

Adds length_sort_straight option #1373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/configuration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
7 changes: 7 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ def _build_arg_parser() -> argparse.ArgumentParser:
dest="length_sort",
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",
Expand Down
5 changes: 4 additions & 1 deletion isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion isort/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}"

Expand Down
23 changes: 23 additions & 0 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this test as os and sys sort the same in both alphabetical and length sort mode. Added datetime that would be first in alphabetical, but last in length sort mode.

"import sys\n"
"import os\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
Expand All @@ -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"
Expand Down