Repository: timothycrosley/isort
Pull request #407 (2016-03-28 11:25:59)

Feature/fix issue 357


Patch:
diff --git a/isort/isort.py b/isort/isort.py
index 20cc96f5..59dbcd01 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -707,10 +707,10 @@ class SortImports(object):
                     self.import_index = self.index - 1
                 continue
 
-            if "isort:" + "imports-" in line and line.startswith("#"):
-                section = line.split("isort:" + "imports-")[-1].split()[0]
-                self.place_imports[section.upper()] = []
-                self.import_placements[line] = section.upper()
+            if "isort:imports-" in line and line.startswith("#"):
+                section = line.split("isort:imports-")[-1].split()[0].upper()
+                self.place_imports[section] = []
+                self.import_placements[line] = section
 
             if ";" in line:
                 for part in (part.strip() for part in line.split(";")):
@@ -796,7 +796,8 @@ class SortImports(object):
 
                     if len(self.out_lines) > max(self.import_index, self._first_comment_index_end, 1) - 1:
                         last = self.out_lines and self.out_lines[-1].rstrip() or ""
-                        while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
+                        while (last.startswith("#") and not last.endswith('"""') and not last.endswith("'''") and not
+                               'isort:imports-' in last):
                             self.comments['above']['from'].setdefault(import_from, []).insert(0, self.out_lines.pop(-1))
                             if len(self.out_lines) > max(self.import_index - 1, self._first_comment_index_end, 1) - 1:
                                 last = self.out_lines[-1].rstrip()
@@ -817,7 +818,8 @@ class SortImports(object):
 
                         if len(self.out_lines) > max(self.import_index, self._first_comment_index_end, 1) - 1:
                             last = self.out_lines and self.out_lines[-1].rstrip() or ""
-                            while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
+                            while (last.startswith("#") and not last.endswith('"""') and not last.endswith("'''")
+                                   and not 'isort:imports-' in last):
                                 self.comments['above']['straight'].setdefault(module, []).insert(0,
                                                                                                  self.out_lines.pop(-1))
                                 if len(self.out_lines) > max(self.import_index - 1, self._first_comment_index_end,


Test patch:
diff --git a/test_isort.py b/test_isort.py
index b15d46ae..22a572f5 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -1232,18 +1232,22 @@ def test_place_comments():
                   "print('code')\n"
                   "\n"
                   "# isort:imports-stdlib\n")
+    expected_output = ("\n# isort:imports-thirdparty\n"
+                       "import django.settings\n"
+                       "\n"
+                       "# isort:imports-firstparty\n"
+                       "import myproject.test\n"
+                       "\n"
+                       "print('code')\n"
+                       "\n"
+                       "# isort:imports-stdlib\n"
+                       "import os\n"
+                       "import sys\n")
     test_output = SortImports(file_contents=test_input, known_third_party=['django']).output
-    assert test_output == ("\n# isort:imports-thirdparty\n"
-                           "import django.settings\n"
-                           "\n"
-                           "# isort:imports-firstparty\n"
-                           "import myproject.test\n"
-                           "\n"
-                           "print('code')\n"
-                           "\n"
-                           "# isort:imports-stdlib\n"
-                           "import os\n"
-                           "import sys\n")
+    assert test_output == expected_output
+    test_output = SortImports(file_contents=test_output, known_third_party=['django']).output
+    assert test_output == expected_output
+
 
 
 def test_placement_control():