Repository: timothycrosley/isort
Pull request #510 (2017-05-15 06:39:27)

Feature/fix issue 299


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bdffa81e..20ea586e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -65,3 +65,6 @@ Changelog
 
 ### 4.2.7
 - Added `--virtual-env` switch command line option
+
+### 4.2.8 - In progress
+- Fixed issue #299, long lines occasionally not wrapped
diff --git a/isort/isort.py b/isort/isort.py
index dceafab5..eb89959d 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -326,7 +326,7 @@ class SortImports(object):
         """
         wrap_mode = self.config.get('multi_line_output', 0)
         if len(line) > self.config['line_length'] and wrap_mode != settings.WrapModes.NOQA:
-            for splitter in ("import", "."):
+            for splitter in ("import", ".", "as"):
                 exp = r"\b" + re.escape(splitter) + r"\b"
                 if re.search(exp, line) and not line.strip().startswith(splitter):
                     line_parts = re.split(exp, line)
@@ -459,7 +459,7 @@ class SortImports(object):
                         indent = self.config['indent']
                         line_length = self.config['wrap_length'] or self.config['line_length']
                         import_statement = formatter(import_start, copy.copy(from_imports),
-                                                    dynamic_indent, indent, line_length, comments)
+                                                     dynamic_indent, indent, line_length, comments)
                         if self.config['balanced_wrapping']:
                             lines = import_statement.split("\n")
                             line_count = len(lines)
@@ -588,8 +588,16 @@ class SortImports(object):
             next_import = imports.pop(0)
             next_statement = self._add_comments(comments, statement + ", " + next_import)
             if len(next_statement.split("\n")[-1]) + 1 > line_length:
+                lines = ['{0}{1}'.format(white_space, next_import.split(" ")[0])]
+                for part in next_import.split(" ")[1:]:
+                    new_line = '{0} {1}'.format(lines[-1], part)
+                    if len(new_line) + 1 > line_length:
+                        lines.append('{0}{1}'.format(white_space, part))
+                    else:
+                        lines[-1] = new_line
+                next_import = '\n'.join(lines)
                 statement = (self._add_comments(comments, "{0},".format(statement)) +
-                             "\n{0}{1}".format(white_space, next_import))
+                             "\n{0}".format(next_import))
                 comments = None
             else:
                 statement += ", " + next_import


Test patch:
diff --git a/test_isort.py b/test_isort.py
index 866ce8f6..72b69d48 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -1927,3 +1927,22 @@ def test_sys_path_mutation():
         assert len(sys.path) == expected_length
     finally:
         shutil.rmtree(tmp_dir, ignore_errors=True)
+
+
+def test_long_single_line():
+    """Test to ensure long single lines get handled correctly"""
+    output = SortImports(file_contents="from ..views import ("
+                                       " _a,"
+                                       "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)",
+                         line_length=79).output
+    for line in output.split('\n'):
+        assert len(line) <= 79
+
+    output = SortImports(file_contents="from ..views import ("
+                                       " _a,"
+                                       "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)",
+                         line_length=76, combine_as_imports=True).output
+    for line in output.split('\n'):
+        assert len(line) <= 79
+
+