Skip to content

Commit

Permalink
Fix issue 957 (#958)
Browse files Browse the repository at this point in the history
* testcase to reproduce

* fix-issue-957
  • Loading branch information
mattbennett authored and timothycrosley committed Jun 19, 2019
1 parent d093f97 commit b9605b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
15 changes: 9 additions & 6 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@ def _wrap(self, line: str) -> str:

cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip())
if self.config['use_parentheses']:
output = "{0}{1}({2}{3}{4}{5})".format(
line, splitter, self.line_separator, cont_line,
"," if self.config['include_trailing_comma'] else "",
self.line_separator if wrap_mode in {WrapModes.VERTICAL_HANGING_INDENT,
WrapModes.VERTICAL_GRID_GROUPED}
else "")
if splitter == "as ":
output = "{0}{1}{2}".format(line, splitter, cont_line.lstrip())
else:
output = "{0}{1}({2}{3}{4}{5})".format(
line, splitter, self.line_separator, cont_line,
"," if self.config['include_trailing_comma'] else "",
self.line_separator if wrap_mode in {WrapModes.VERTICAL_HANGING_INDENT,
WrapModes.VERTICAL_GRID_GROUPED}
else "")
lines = output.split(self.line_separator)
if self.config['comment_prefix'] in lines[-1] and lines[-1].endswith(')'):
line, comment = lines[-1].split(self.config['comment_prefix'], 1)
Expand Down
23 changes: 23 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,29 @@ def test_alias_using_paren_issue_466():
use_parentheses=True).output == expected_output


def test_long_alias_using_paren_issue_957():
test_input = ('from package import module as very_very_very_very_very_very_very_very_very_very_long_alias\n')
expected_output = ('from package import (\n'
' module as very_very_very_very_very_very_very_very_very_very_long_alias\n'
')\n')
out = SortImports(file_contents=test_input, line_length=50, use_parentheses=True, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, check=True).output
assert out == expected_output

test_input = ('from deep.deep.deep.deep.deep.deep.deep.deep.deep.package import module as very_very_very_very_very_very_very_very_very_very_long_alias\n')
expected_output = ('from deep.deep.deep.deep.deep.deep.deep.deep.deep.package import (\n'
' module as very_very_very_very_very_very_very_very_very_very_long_alias\n'
')\n')
out = SortImports(file_contents=test_input, line_length=50, use_parentheses=True, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, check=True).output
assert out == expected_output

test_input = ('from deep.deep.deep.deep.deep.deep.deep.deep.deep.package import very_very_very_very_very_very_very_very_very_very_long_module as very_very_very_very_very_very_very_very_very_very_long_alias\n')
expected_output = ('from deep.deep.deep.deep.deep.deep.deep.deep.deep.package import (\n'
' very_very_very_very_very_very_very_very_very_very_long_module as very_very_very_very_very_very_very_very_very_very_long_alias\n'
')\n')
out = SortImports(file_contents=test_input, line_length=50, use_parentheses=True, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, check=True).output
assert out == expected_output


def test_strict_whitespace_by_default(capsys):
test_input = ('import os\n'
'from django.conf import settings\n')
Expand Down

0 comments on commit b9605b5

Please sign in to comment.