summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2019-02-23 14:22:07 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2019-02-23 14:22:07 -0800
commit4ee2fa80f4fe5a1441309a9620aa0e9f8cf05f38 (patch)
tree6c1d66e63c46bc937e1e7f927515fefebc33e965
parentb03c5b7506ec9307b4d2c37dec2f2a075c8f9fc8 (diff)
downloadisort-4ee2fa80f4fe5a1441309a9620aa0e9f8cf05f38.tar.gz
Fix issue 576
-rw-r--r--isort/isort.py4
-rw-r--r--test_isort.py7
2 files changed, 11 insertions, 0 deletions
diff --git a/isort/isort.py b/isort/isort.py
index 970117f1..7c915e97 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -400,6 +400,10 @@ class SortImports(object):
while from_imports and from_imports[0] in as_imports:
from_import = from_imports.pop(0)
from_comments = self.comments['straight'].get('{}.{}'.format(module, from_import))
+ above_comments = self.comments['above']['from'].pop(module, None)
+ if above_comments:
+ section_output.extend(above_comments)
+
section_output.append(self._add_comments(from_comments,
self._wrap(import_start + as_imports[from_import])))
diff --git a/test_isort.py b/test_isort.py
index 51b34357..bdd4453c 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2708,3 +2708,10 @@ def test_safety_excludes(tmpdir, enabled):
else:
assert file_names == {'.tox/verysafe.py', 'lib/python3.7/importantsystemlibrary.py', 'victim.py'}
assert not skipped
+
+
+def test_comments_not_removed_issue_576():
+ test_input = ('import distutils\n'
+ '# this comment is important and should not be removed\n'
+ 'from sys import api_version as api_version\n')
+ assert SortImports(file_contents=test_input).output == test_input