summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2016-03-28 04:30:06 -0700
committerTimothy Edmund Crosley <timothy.crosley@gmail.com>2016-03-28 04:30:06 -0700
commite6c339e49eaa5d3341cfd8a90d20c88804b790ce (patch)
tree7546b0228781bf960bfa12dd9c789ea4c540b7d8
parent9469519ab6bca7b39a2db82bdc361cdb952c9d5d (diff)
parent8ca8bf0f0b34ae3901bec96eb6beaa02701610aa (diff)
downloadisort-e6c339e49eaa5d3341cfd8a90d20c88804b790ce.tar.gz
Merge pull request #407 from timothycrosley/feature/fix-issue-357
Feature/fix issue 357
-rw-r--r--isort/isort.py14
-rw-r--r--test_isort.py26
2 files changed, 23 insertions, 17 deletions
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,
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():