Skip to content

Commit

Permalink
Fixed #1395: reverse_relative setting doesn't have any effect when co…
Browse files Browse the repository at this point in the history
…mbined with force_sort_within_sections.
  • Loading branch information
timothycrosley committed Aug 22, 2020
1 parent ff1ccda commit ec63f34
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Changelog

NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.5.0 TBD
- Fixed #1398: isort: off comment doesn't work, if it's the top comment in the file.
- Fixed #1395: reverse_relative setting doesn't have any effect when combined with force_sort_within_sections.

### 5.4.2 Aug 14, 2020
- Fixed #1383: Known other does not work anymore with .editorconfig.
- Fixed: Regression in first known party path expansion.
Expand Down
1 change: 1 addition & 0 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def sorted_imports(
force_to_top=config.force_to_top,
lexicographical=config.lexicographical,
length_sort=config.length_sort,
reverse_relative=config.reverse_relative,
),
)

Expand Down
6 changes: 6 additions & 0 deletions isort/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ def section_key(
force_to_top: List[str],
lexicographical: bool = False,
length_sort: bool = False,
reverse_relative: bool = False,
) -> str:
section = "B"

if reverse_relative and line.startswith("from ."):
match = re.match(r"^from (\.+)\s*(.*)", line)
if match:
line = f"from {' '.join(match.groups())}"

if lexicographical:
line = _import_line_intro_re.sub("", _import_line_midline_import_re.sub(".", line))
else:
Expand Down
39 changes: 31 additions & 8 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,26 +541,49 @@ def test_incorrect_grouping_when_comments_issue_1396():
"""Test to ensure isort groups import correct independent of the comments present.
See: https://github.com/timothycrosley/isort/issues/1396
"""
assert isort.code(
"""from django.shortcuts import render
assert (
isort.code(
"""from django.shortcuts import render
from apps.profiler.models import Project
from django.contrib.auth.decorators import login_required
from django.views.generic import (
# ListView,
# ListView,
# DetailView,
TemplateView,
# CreateView,
# View
)
""",
line_length=88,
known_first_party=["apps"],
known_django=["django"],
sections=["FUTURE", "STDLIB", "DJANGO", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
) == """from django.contrib.auth.decorators import login_required
line_length=88,
known_first_party=["apps"],
known_django=["django"],
sections=["FUTURE", "STDLIB", "DJANGO", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"],
)
== """from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.views.generic import \\
TemplateView # ListView,; DetailView,; CreateView,; View
from apps.profiler.models import Project
"""
)


def test_reverse_relative_combined_with_force_sort_within_sections_issue_1395():
"""Test to ensure reverse relative combines well with other common isort settings.
See: https://github.com/timothycrosley/isort/issues/1395.
"""
assert isort.check_code(
"""from .fileA import a_var
from ..fileB import b_var
""",
show_diff=True,
reverse_relative=True,
force_sort_within_sections=True,
order_by_type=False,
case_sensitive=False,
multi_line_output=5,
sections=["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "APPLICATION", "LOCALFOLDER"],
lines_after_imports=2,
no_lines_before="LOCALFOLDER",
)

0 comments on commit ec63f34

Please sign in to comment.