From f54c0d76825781977a29aa1680e68d66d72617fa Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 1 Feb 2018 22:28:00 -0800 Subject: Add test case for issue #654 --- test_isort.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test_isort.py b/test_isort.py index db505647..147c2e5f 100644 --- a/test_isort.py +++ b/test_isort.py @@ -153,6 +153,13 @@ def test_line_length(): " lib18, lib20,\n" " lib21, lib22)\n") + TEST_INPUT = ('from django.contrib.gis.gdal.field import (\n' + ' OFTDate, OFTDateTime, OFTInteger, OFTInteger64, OFTReal, OFTString,\n' + ' OFTTime,\n' + ')\n') # Test case described in issue #654 + assert SortImports(file_contents=TEST_INPUT, include_trailing_comma=True, line_length=79, + multi_line_output=WrapModes.VERTICAL_GRID_GROUPED).output == TEST_INPUT + test_output = SortImports(file_contents=REALLY_LONG_IMPORT, line_length=42, wrap_length=32).output assert test_output == ("from third_party import (lib1,\n" " lib2,\n" -- cgit v1.2.1 From ef2214909294c143f0b549aa9d03e2e8ec8c8bc5 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 1 Feb 2018 22:39:20 -0800 Subject: Implement fix for issue #654 while still supporting mode described in pull request #631 --- isort/isort.py | 14 ++++++++++---- isort/main.py | 2 +- isort/settings.py | 3 ++- test_isort.py | 9 ++++++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index ecb2bf24..0a672db3 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -699,7 +699,8 @@ class SortImports(object): "," if self.config['include_trailing_comma'] else "", ) - def _output_vertical_grid_common(self, statement, imports, white_space, indent, line_length, comments, need_trailing_char): + def _output_vertical_grid_common(self, statement, imports, white_space, indent, line_length, comments, + need_trailing_char): statement += self._add_comments(comments, "(") + self.line_separator + indent + imports.pop(0) while imports: next_import = imports.pop(0) @@ -717,11 +718,16 @@ class SortImports(object): return statement def _output_vertical_grid(self, statement, imports, white_space, indent, line_length, comments): - return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, True) + ")" + return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, + True) + ")" def _output_vertical_grid_grouped(self, statement, imports, white_space, indent, line_length, comments): - return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, False) \ - + self.line_separator + ")" + return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, + True) + self.line_separator + ")" + + def _output_vertical_grid_grouped_no_comma(self, statement, imports, white_space, indent, line_length, comments): + return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, + False) + self.line_separator + ")" def _output_noqa(self, statement, imports, white_space, indent, line_length, comments): retval = '{0}{1}'.format(statement, ', '.join(imports)) diff --git a/isort/main.py b/isort/main.py index 8c748b20..efb3793d 100755 --- a/isort/main.py +++ b/isort/main.py @@ -230,7 +230,7 @@ def create_parser(): dest='length_sort', action='store_true') 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).') + '5-vert-grid-grouped, 6-vert-grid-grouped-no-comma).') inline_args_group.add_argument('-nis', '--no-inline-sort', dest='no_inline_sort', action='store_true', help='Leaves `from` imports with multiple imports \'as-is\' (e.g. `from foo import a, c ,b`).') parser.add_argument('-nlb', '--no-lines-before', help='Sections which should not be split with previous by empty lines', diff --git a/isort/settings.py b/isort/settings.py index 09feb5dc..dd0a7110 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -41,7 +41,8 @@ except ImportError: MAX_CONFIG_SEARCH_DEPTH = 25 # The number of parent directories isort will look for a config file within DEFAULT_SECTIONS = ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER') -WrapModes = ('GRID', 'VERTICAL', 'HANGING_INDENT', 'VERTICAL_HANGING_INDENT', 'VERTICAL_GRID', 'VERTICAL_GRID_GROUPED', 'NOQA') +WrapModes = ('GRID', 'VERTICAL', 'HANGING_INDENT', 'VERTICAL_HANGING_INDENT', 'VERTICAL_GRID', 'VERTICAL_GRID_GROUPED', + 'VERTICAL_GRID_GROUPED_NO_COMMA', 'NOQA') WrapModes = namedtuple('WrapModes', WrapModes)(*range(len(WrapModes))) # Note that none of these lists must be complete as they are simply fallbacks for when included auto-detection fails. diff --git a/test_isort.py b/test_isort.py index 147c2e5f..429380cb 100644 --- a/test_isort.py +++ b/test_isort.py @@ -158,7 +158,7 @@ def test_line_length(): ' OFTTime,\n' ')\n') # Test case described in issue #654 assert SortImports(file_contents=TEST_INPUT, include_trailing_comma=True, line_length=79, - multi_line_output=WrapModes.VERTICAL_GRID_GROUPED).output == TEST_INPUT + multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, balanced_wrapping=False).output == TEST_INPUT test_output = SortImports(file_contents=REALLY_LONG_IMPORT, line_length=42, wrap_length=32).output assert test_output == ("from third_party import (lib1,\n" @@ -360,10 +360,13 @@ def test_output_modes(): output_noqa = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.NOQA).output - assert output_noqa == "from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22 # NOQA comment\n" # NOQA + assert output_noqa == ("from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11," + " lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22 " + "# NOQA comment\n") test_output_vertical_grid_grouped_doesnt_wrap_early = SortImports(file_contents=SINGLE_LINE_LONG_IMPORT, - multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, + multi_line_output= \ + WrapModes.VERTICAL_GRID_GROUPED_NO_COMMA, line_length=40, indent=' ').output assert test_output_vertical_grid_grouped_doesnt_wrap_early == ("from third_party import (\n" " lib1, lib2, lib3, lib4, lib5, lib5ab\n" -- cgit v1.2.1 From 1db36205f016ae4b72bb713aedf7f96b1be43723 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 1 Feb 2018 22:43:36 -0800 Subject: Fix indentation erro --- test_isort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_isort.py b/test_isort.py index 429380cb..56cf85fe 100644 --- a/test_isort.py +++ b/test_isort.py @@ -364,10 +364,10 @@ def test_output_modes(): " lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22 " "# NOQA comment\n") - test_output_vertical_grid_grouped_doesnt_wrap_early = SortImports(file_contents=SINGLE_LINE_LONG_IMPORT, - multi_line_output= \ - WrapModes.VERTICAL_GRID_GROUPED_NO_COMMA, - line_length=40, indent=' ').output + test_case = SortImports(file_contents=SINGLE_LINE_LONG_IMPORT, + multi_line_output=WrapModes.VERTICAL_GRID_GROUPED_NO_COMMA, + line_length=40, indent=' ').output + test_output_vertical_grid_grouped_doesnt_wrap_early = test_case assert test_output_vertical_grid_grouped_doesnt_wrap_early == ("from third_party import (\n" " lib1, lib2, lib3, lib4, lib5, lib5ab\n" ")\n") -- cgit v1.2.1