From ec63f343666ca18fd8efa9fad07438f996da2cf6 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 21 Aug 2020 22:48:56 -0700 Subject: [PATCH] Fixed #1395: reverse_relative setting doesn't have any effect when combined with force_sort_within_sections. --- CHANGELOG.md | 4 ++++ isort/output.py | 1 + isort/sorting.py | 6 ++++++ tests/test_regressions.py | 39 +++++++++++++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7a31d901..abcc9b37c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/isort/output.py b/isort/output.py index 6a21c1315..2a918323d 100644 --- a/isort/output.py +++ b/isort/output.py @@ -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, ), ) diff --git a/isort/sorting.py b/isort/sorting.py index 1664a2f28..780747a3d 100644 --- a/isort/sorting.py +++ b/isort/sorting.py @@ -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: diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 3c302e180..4424f9e81 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -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", + )