Repository: timothycrosley/isort
Pull request #426 (2016-04-16 18:42:37)

Fix hanging imports being removed
As mentioned in https://github.com/timothycrosley/isort/issues/425
This fixes hanging imports being removed by isort.

Code was missing a whitespace when glueing the import line together


Patch:
diff --git a/ACKNOWLEDGEMENTS.md b/ACKNOWLEDGEMENTS.md
index f77527d3..99c7873a 100644
--- a/ACKNOWLEDGEMENTS.md
+++ b/ACKNOWLEDGEMENTS.md
@@ -67,7 +67,6 @@ Code Contributors
 - Dan Palmer (@danpalmer)
 - Andy Boot (@bootandy)
 - @m7v8
-- John Vandenberg (@jayvdb)
 
 Documenters
 ===================
diff --git a/appveyor.yml b/appveyor.yml
deleted file mode 100644
index 65e0d282..00000000
--- a/appveyor.yml
+++ /dev/null
@@ -1,12 +0,0 @@
-init:
-  - "SET PATH=C:\\Python27\\Scripts;%PATH%"
-  - "ECHO PATH=%PATH%"
-  - python --version
-
-build: off
-
-install:
-  - pip install tox
-
-test_script:
-  - tox -e isort-check,py26,py27,py33,py34,py35
diff --git a/isort/isort.py b/isort/isort.py
index fe63766b..80e46452 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -418,21 +418,10 @@ class SortImports(object):
                         import_statement = self._add_comments(comments, import_start + (", ").join(from_imports))
                     if not from_imports:
                         import_statement = ""
-
-                    do_multiline_reformat = False
-
-                    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:
-                        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
-                        and self.config.get('multi_line_output', 0) not in (1, 0)):
-                        do_multiline_reformat = True
-
-                    if do_multiline_reformat:
+                    if len(from_imports) > 1 and (
+                        len(import_statement) > self.config['line_length']
+                        or self.config.get('force_grid_wrap')
+                    ):
                         output_mode = settings.WrapModes._fields[self.config.get('multi_line_output',
                                                                                     0)].lower()
                         formatter = getattr(self, "_output_" + output_mode, self._output_grid)
@@ -456,8 +445,7 @@ class SortImports(object):
                                 new_import_statement = formatter(import_start, copy.copy(from_imports),
                                                                 dynamic_indent, indent, line_length, comments)
                                 lines = new_import_statement.split("\n")
-
-                    if not do_multiline_reformat and len(import_statement) > self.config['line_length']:
+                    elif len(import_statement) > self.config['line_length']:
                         import_statement = self._wrap(import_statement)
 
                 if import_statement:
@@ -788,7 +776,7 @@ class SortImports(object):
                         if import_string.strip().endswith(" import") or line.strip().startswith("import "):
                             import_string += "\n" + line
                         else:
-                            import_string = import_string.rstrip().rstrip("\\") + line.lstrip()
+                            import_string = import_string.rstrip().rstrip("\\") + " " + line.lstrip()
 
                 if import_type == "from":
                     import_string = import_string.replace("import(", "import (")


Test patch:
diff --git a/test_isort.py b/test_isort.py
index 11ea6a18..5fb9795a 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -768,7 +768,6 @@ def test_force_single_line_long_imports():
     assert test_output == ("from veryveryveryveryveryvery import big\n"
                            "from veryveryveryveryveryvery import small  # NOQA\n")
 
-
 def test_titled_imports():
     """Tests setting custom titled/commented import sections."""
     test_input = ("import sys\n"
@@ -828,6 +827,20 @@ def test_multiline_import():
     assert SortImports(file_contents=test_input, **custom_configuration).output == expected_output
 
 
+def test_single_multiline():
+    """Test the case where a single import spawns multiple lines."""
+    test_input = ("from os import\\\n"
+                  "        getuid\n"
+                  "\n"
+                  "print getuid()\n")
+    output = SortImports(file_contents=test_input).output
+    assert output == (
+        "from os import getuid\n"
+        "\n"
+        "print getuid()\n"
+    )
+
+
 def test_atomic_mode():
     # without syntax error, everything works OK
     test_input = ("from b import d, c\n"
@@ -1459,14 +1472,15 @@ def test_fcntl():
 
 def test_import_split_is_word_boundary_aware():
     """Test to ensure that isort splits words in a boundry aware mannor"""
-    test_input = ("from mycompany.model.size_value_array_import_func import \\\n"
-                "    get_size_value_array_import_func_jobs")
+    test_input = ("from mycompany.model.size_value_array_import_func import ("
+                "    get_size_value_array_import_func_jobs,"
+                ")")
     test_output = SortImports(file_contents=test_input,
       multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
       line_length=79).output
-    assert test_output == ("from mycompany.model.size_value_array_import_func import (\n"
-                           "    get_size_value_array_import_func_jobs\n"
-                           ")\n")
+
+    assert test_output == ("from mycompany.model.size_value_array_import_func import \\\n"
+                           "    get_size_value_array_import_func_jobs\n")
 
 
 def test_other_file_encodings():