Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add always_wrap_multiple_from setting. #469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions isort/isort.py
Expand Up @@ -430,11 +430,14 @@ def _add_from_imports(self, from_modules, section, section_output, ignore_case):
if self.config.get('force_grid_wrap') and len(from_imports) > 1:
do_multiline_reformat = True

if len(import_statement) > self.config['line_length'] and len(from_imports) > 1:
long_line = len(import_statement) > self.config['line_length']
always_wrap_multiple_from = self.config.get('always_wrap_multiple_from', False)

if (long_line or always_wrap_multiple_from) and len(from_imports) > 1:
do_multiline_reformat = True

# If line too long AND have imports AND we are NOT using GRID or VERTICAL wrap modes
if (len(import_statement) > self.config['line_length'] and len(from_imports) > 0
if (long_line and len(from_imports) > 0
and self.config.get('multi_line_output', 0) not in (1, 0)):
do_multiline_reformat = True

Expand Down
2 changes: 2 additions & 0 deletions isort/main.py
Expand Up @@ -167,6 +167,8 @@ def create_parser():
parser.add_argument('-m', '--multi_line', dest='multi_line_output', type=int, choices=[0, 1, 2, 3, 4, 5],
help='Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, '
'5-vert-grid-grouped).')
parser.add_argument('-ma', '--always_wrap_multiple_from', dest='always_wrap_multiple_from', action='store_true',
default=False, help='Always use multi-line mode if there are multiple from imports')
parser.add_argument('-i', '--indent', help='String to place for indents defaults to " " (4 spaces).',
dest='indent', type=str)
parser.add_argument('-a', '--add_import', dest='add_imports', action='append',
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Expand Up @@ -73,6 +73,7 @@
'known_third_party': ['google.appengine.api'],
'known_first_party': [],
'multi_line_output': WrapModes.GRID,
'always_wrap_multiple_from': False,
'forced_separate': [],
'indent': ' ' * 4,
'length_sort': False,
Expand Down
70 changes: 70 additions & 0 deletions test_isort.py
Expand Up @@ -1114,6 +1114,76 @@ def test_include_trailing_comma():
)


def test_always_wrap_multiple_from():
"""Test for the always_wrap_multiple_from option"""
test_output_grid = SortImports(
file_contents=SHORT_IMPORT,
multi_line_output=WrapModes.GRID,
line_length=80,
wrap_length=40,
always_wrap_multiple_from=True,
).output
assert test_output_grid == (
"from third_party import (lib1, lib2,\n"
" lib3, lib4)\n"
)

test_output_vertical = SortImports(
file_contents=SHORT_IMPORT,
multi_line_output=WrapModes.VERTICAL,
line_length=80,
wrap_length=40,
always_wrap_multiple_from=True,
).output
assert test_output_vertical == (
"from third_party import (lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4)\n"
)

test_output_vertical_indent = SortImports(
file_contents=SHORT_IMPORT,
multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
line_length=80,
wrap_length=40,
always_wrap_multiple_from=True,
).output
assert test_output_vertical_indent == (
"from third_party import (\n"
" lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4\n"
")\n"
)

test_output_vertical_grid = SortImports(
file_contents=SHORT_IMPORT,
multi_line_output=WrapModes.VERTICAL_GRID,
line_length=80,
wrap_length=40,
always_wrap_multiple_from=True,
).output
assert test_output_vertical_grid == (
"from third_party import (\n"
" lib1, lib2, lib3, lib4)\n"
)

test_output_vertical_grid_grouped = SortImports(
file_contents=SHORT_IMPORT,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
line_length=80,
wrap_length=40,
always_wrap_multiple_from=True,
).output
assert test_output_vertical_grid_grouped == (
"from third_party import (\n"
" lib1, lib2, lib3, lib4\n"
")\n"
)


def test_similar_to_std_library():
"""Test to ensure modules that are named similarly to a standard library import don't end up clobbered"""
test_input = ("import datetime\n"
Expand Down