diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2015-02-05 23:35:56 -0500 |
---|---|---|
committer | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2015-02-05 23:35:56 -0500 |
commit | b5ca0f6fba29c347bcb6de149d50f6ab947f4128 (patch) | |
tree | 623a51ba0c744c6c143e4f845120c8fd37389315 | |
parent | 947adf17b343a13f8ebf14401d007a8031259379 (diff) | |
parent | e77354f122add0ce8d317263fd4cb697feed20a1 (diff) | |
download | isort-b5ca0f6fba29c347bcb6de149d50f6ab947f4128.tar.gz |
Merge pull request #247 from timothycrosley/feature/fix-issue-246
Feature/fix issue 246
-rw-r--r-- | isort/isort.py | 21 | ||||
-rw-r--r-- | test_isort.py | 8 |
2 files changed, 21 insertions, 8 deletions
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) 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""" |