From bd8b38542d5645762f1d0ecdde4f34da6b32ef02 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 5 Feb 2015 11:59:33 -0500 Subject: Fix issue #238 --- isort/isort.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index 038cd3d0..f8dbd108 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -620,6 +620,7 @@ class SortImports(object): while not self._at_end(): line = self._get_line() skip_line = self._skip_line(line) + on_first_import = False if line in self._section_comments and not skip_line: if self.import_index == -1: @@ -650,6 +651,7 @@ class SortImports(object): line = line.replace("\t", " ") if self.import_index == -1: self.import_index = self.index - 1 + on_first_import = True nested_comments = {} import_string, comments, new_comments = self._strip_comments(line) @@ -706,7 +708,8 @@ class SortImports(object): if comments: self.comments['from'].setdefault(import_from, []).extend(comments) last = self.out_lines and self.out_lines[-1].rstrip() or "" - if last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"): + if (not on_first_import 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 root.get(import_from, False): root[import_from].update(imports) @@ -718,6 +721,7 @@ class SortImports(object): self.comments['straight'][module] = comments comments = None last = self.out_lines and self.out_lines[-1].rstrip() or "" - if last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"): + if (not on_first_import and last.startswith("#") and not + last.endswith('"""') and not last.endswith("'''")): self.comments['above']['from'][module] = self.out_lines.pop(-1) self.imports[self.place_module(module)][import_type].add(module) -- cgit v1.2.1