Repository: timothycrosley/isort (python)

Issue:
isort removes comments between imports
If the following code is processed with isort the comment is removed.

```python
import distutils
# this comment is important and should not be removed 
from sys import api_version as api_version
```

This was reported in https://github.com/coala/coala-bears/issues/1929 as we use isort through coala. 

I'm not even sure what the intended behavior is here, since sorting imports with comments could raise all kinds of problems, but removing them seems wrong too. 

Have you thought about this problem yet? 

Pull request description:
Fix issue 576
Fixes #576 

Patch:
diff --git a/isort/isort.py b/isort/isort.py
index 970117f1..df075bc2 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])))
 


Test patch:
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