Repository: timothycrosley/isort
Pull request #247 (2015-02-06 04:32:03)

Feature/fix issue 246


Patch:
diff --git a/isort/isort.py b/isort/isort.py
index 8ce51a4d..34ae27d0 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -720,10 +720,13 @@ class SortImports(object):
                             comments.pop(comments.index(associated_commment))
                     if comments:
                         self.comments['from'].setdefault(import_from, []).extend(comments)
-                    last = self.out_lines and self.out_lines[-1].rstrip() or ""
-                    if (len(self.out_lines) > self.import_index and last.startswith("#") and not last.endswith('"""') and not
-                        last.endswith("'''")):
-                        self.comments['above']['from'].setdefault(import_from, []).append(self.out_lines.pop(-1))
+
+                    if len(self.out_lines) > self.import_index:
+                        last = self.out_lines and self.out_lines[-1].rstrip() or ""
+                        while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
+                            self.comments['above']['from'].setdefault(import_from, []).insert(0, self.out_lines.pop(-1))
+                            last = self.out_lines and self.out_lines[-1].rstrip() or ""
+
                     if root.get(import_from, False):
                         root[import_from].update(imports)
                     else:
@@ -733,8 +736,10 @@ class SortImports(object):
                         if comments:
                             self.comments['straight'][module] = comments
                             comments = None
-                        last = self.out_lines and self.out_lines[-1].rstrip() or ""
-                        if (len(self.out_lines) > self.import_index and last.startswith("#") and not
-                            last.endswith('"""') and not last.endswith("'''")):
-                            self.comments['above']['from'][module] = self.out_lines.pop(-1)
+
+                        if len(self.out_lines) > self.import_index:
+                            last = self.out_lines and self.out_lines[-1].rstrip() or ""
+                            while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
+                                self.comments['above']['from'].setdefault(module, []).insert(0, self.out_lines.pop(-1))
+                                last = self.out_lines and self.out_lines[-1].rstrip() or ""
                         self.imports[self.place_module(module)][import_type].add(module)


Test patch:
diff --git a/test_isort.py b/test_isort.py
index 2aeb474c..de345c46 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -1206,6 +1206,14 @@ def test_sticky_comments():
                   "from selenium.webdriver.remote.webdriver import WebDriver  # noqa\n")
     assert SortImports(file_contents=test_input).output == test_input
 
+    test_input = ("from django import forms\n"
+                  "# While this couples the geographic forms to the GEOS library,\n"
+                  "# it decouples from database (by not importing SpatialBackend).\n"
+                  "from django.contrib.gis.geos import GEOSException, GEOSGeometry\n"
+                  "from django.utils.translation import ugettext_lazy as _\n")
+    assert SortImports(file_contents=test_input).output == test_input
+
+
 
 def test_zipimport():
     """Imports ending in "import" shouldn't be clobbered"""