Skip to content

Commit

Permalink
Merge pull request #1664 from gofr/issue/1659/force-sort-sections-wit…
Browse files Browse the repository at this point in the history
…h-relative-imports

Sort relative imports correctly with force_sort_within_sections
  • Loading branch information
timothycrosley committed Mar 11, 2021
2 parents 6b0b85f + 88006f9 commit f76ed04
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions isort/main.py
Expand Up @@ -688,6 +688,14 @@ def _build_arg_parser() -> argparse.ArgumentParser:
help="Honor `--case-sensitive` when `--force-sort-within-sections` is being used. "
"Without this option set, `--order-by-type` decides module name ordering too.",
)
section_group.add_argument(
"--srss",
"--sort-relative-in-force-sorted-sections",
action="store_true",
dest="sort_relative_in_force_sorted_sections",
help="When using `--force-sort-within-sections`, sort relative imports the same "
"way as they are sorted when not using that setting.",
)
section_group.add_argument(
"--fass",
"--force-alphabetical-sort-within-sections",
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Expand Up @@ -206,6 +206,7 @@ class _Config:
follow_links: bool = True
indented_import_headings: bool = True
honor_case_in_force_sorted_sections: bool = False
sort_relative_in_force_sorted_sections: bool = False
overwrite_in_place: bool = False

def __post_init__(self):
Expand Down
9 changes: 8 additions & 1 deletion isort/sorting.py
Expand Up @@ -54,7 +54,11 @@ def module_key(
def section_key(line: str, config: Config) -> str:
section = "B"

if config.reverse_relative and line.startswith("from ."):
if (
not config.sort_relative_in_force_sorted_sections
and config.reverse_relative
and line.startswith("from .")
):
match = re.match(r"^from (\.+)\s*(.*)", line)
if match: # pragma: no cover - regex always matches if line starts with "from ."
line = f"from {' '.join(match.groups())}"
Expand All @@ -66,6 +70,9 @@ def section_key(line: str, config: Config) -> str:
else:
line = re.sub("^from ", "", line)
line = re.sub("^import ", "", line)
if config.sort_relative_in_force_sorted_sections:
sep = " " if config.reverse_relative else "_"
line = re.sub(r"^(\.+)", fr"\1{sep}", line)
if line.split(" ")[0] in config.force_to_top:
section = "A"
# * If honor_case_in_force_sorted_sections is true, and case_sensitive and
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/test_isort.py
Expand Up @@ -2914,6 +2914,74 @@ def test_sort_within_sections_with_force_to_top_issue_473() -> None:
)


def test_force_sort_within_sections_with_relative_imports() -> None:
"""Test sorting of relative imports with force_sort_within_sections=True"""
assert isort.check_code(
"""import .
from . import foo
from .. import a
from ..alpha.beta import b
from ..omega import c
import .apple as bar
from .mango import baz
""",
show_diff=True,
force_sort_within_sections=True,
)


def test_force_sort_within_sections_with_reverse_relative_imports() -> None:
"""Test reverse sorting of relative imports with force_sort_within_sections=True"""
assert isort.check_code(
"""import .
from . import foo
from .mango import baz
from ..alpha.beta import b
from .. import a
from ..omega import c
import .apple as bar
""",
show_diff=True,
force_sort_within_sections=True,
reverse_relative=True,
)


def test_sort_relative_in_force_sorted_sections_issue_1659() -> None:
"""Ensure relative imports are sorted within sections"""
assert isort.check_code(
"""from .. import a
from ..alpha.beta import b
from ..omega import c
import .
from . import foo
import .apple as bar
from .mango import baz
""",
show_diff=True,
force_sort_within_sections=True,
sort_relative_in_force_sorted_sections=True,
)


def test_reverse_sort_relative_in_force_sorted_sections_issue_1659() -> None:
"""Ensure reverse ordered relative imports are sorted within sections"""
assert isort.check_code(
"""import .
from . import foo
import .apple as bar
from .mango import baz
from .. import a
from ..alpha.beta import b
from ..omega import c
""",
show_diff=True,
force_sort_within_sections=True,
sort_relative_in_force_sorted_sections=True,
reverse_relative=True,
)


def test_correct_number_of_new_lines_with_comment_issue_435() -> None:
"""Test to ensure that injecting a comment in-between imports
doesn't mess up the new line spacing
Expand Down

0 comments on commit f76ed04

Please sign in to comment.