Skip to content

Commit

Permalink
Merge pull request #867 from jpgrayson/develop
Browse files Browse the repository at this point in the history
Add -rr/--reverse-relative option (issue #417)
  • Loading branch information
timothycrosley authored Mar 2, 2019
2 parents d39ab9b + e60c651 commit 12588b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
11 changes: 4 additions & 7 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,10 @@ def _module_key(
ignore_case: bool = False,
section_name: Optional[Any] = None
) -> str:
dots = 0
while module_name.startswith('.'):
dots += 1
module_name = module_name[1:]

if dots:
module_name = '{} {}'.format(('.' * dots), module_name)
match = re.match(r'^(\.+)\s*(.*)', module_name)
if match:
sep = ' ' if config['reverse_relative'] else '_'
module_name = sep.join(match.groups())

prefix = ""
if ignore_case:
Expand Down
2 changes: 2 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
parser.add_argument('-r', dest='ambiguous_r_flag', action='store_true')
parser.add_argument('-rm', '--remove-import', dest='remove_imports', action='append',
help='Removes the specified import from all files.')
parser.add_argument('-rr', '--reverse-relative', dest='reverse_relative', action='store_true',
help='Reverse order of relative imports.')
parser.add_argument('-rc', '--recursive', dest='recursive', action='store_true',
help='Recursively look for Python files of which to sort imports')
parser.add_argument('-s', '--skip', help='Files that sort imports should skip over. If you want to skip multiple '
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def from_string(value: str) -> 'WrapModes':
'length_sort': False,
'add_imports': [],
'remove_imports': [],
'reverse_relative': False,
'force_single_line': False,
'default_section': 'FIRSTPARTY',
'import_heading_future': '',
Expand Down
20 changes: 19 additions & 1 deletion test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ def test_comments_not_removed_issue_576():
assert SortImports(file_contents=test_input).output == test_input


def test_inconsistent_relative_imports_issue_577():
def test_reverse_relative_imports_issue_417():
test_input = ('from . import ipsum\n'
'from . import lorem\n'
'from .dolor import consecteur\n'
Expand All @@ -2766,6 +2766,24 @@ def test_inconsistent_relative_imports_issue_577():
'from ... import dui\n'
'from ...eu import dignissim\n'
'from ...ex import metus\n')
assert SortImports(file_contents=test_input,
force_single_line=True,
reverse_relative=True).output == test_input


def test_inconsistent_relative_imports_issue_577():
test_input = ('from ... import diam\n'
'from ... import dui\n'
'from ...eu import dignissim\n'
'from ...ex import metus\n'
'from .. import donec\n'
'from .. import euismod\n'
'from ..mi import iaculis\n'
'from ..nec import tempor\n'
'from . import ipsum\n'
'from . import lorem\n'
'from .dolor import consecteur\n'
'from .sit import apidiscing\n')
assert SortImports(file_contents=test_input, force_single_line=True).output == test_input


Expand Down

0 comments on commit 12588b4

Please sign in to comment.