Repository: timothycrosley/isort
Pull request #538 (2017-06-01 04:23:15)

Feature/fix issue 435


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cbf5ef28..060b4a35 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ IMPORTANT NOTE: This will be the last release with Python 2.6 support, subsequen
 - Fixed issue #436: Force sort within section no longer rearranges comments
 - Fixed issue #473: Force_to_top and force_sort_within_sections now work together
 - Fixed issue #484 & #472: Consistent output with imports of same spelling but different case
+- Fixed issue #433: No longer incorrectly add an extra new-line when comment between imports and function definition
 Breaking Changes:
     - Fixed issue #511: All command line options with an underscore, have had the underscore replaced with a dash for consistency. This effects: multi-line, add-import, remove-import, force-adds, --force-single-line-imports, and length-sort.
     - Replaced the `--enforce-whitespace` option with `--ignore-whitespace` to restore original behavior of strict whitespace by default
diff --git a/isort/isort.py b/isort/isort.py
index d07b4f17..92340802 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -579,8 +579,11 @@ class SortImports(object):
         if len(self.out_lines) > imports_tail:
             next_construct = ""
             self._in_quote = False
-            for line in self.out_lines[imports_tail:]:
-                if not self._skip_line(line) and not line.strip().startswith("#") and line.strip():
+            tail = self.out_lines[imports_tail:]
+            for index, line in enumerate(tail):
+                if not self._skip_line(line) and line.strip():
+                    if line.strip().startswith("#") and len(tail) > (index + 1) and tail[index + 1].strip():
+                        continue
                     next_construct = line
                     break
 
diff --git a/isort/pie_slice.py b/isort/pie_slice.py
index c0b3cd8b..5079990f 100644
--- a/isort/pie_slice.py
+++ b/isort/pie_slice.py
@@ -22,9 +22,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 from __future__ import absolute_import
 
 import abc
+import collections
 import functools
 import sys
-import collections
 from numbers import Integral
 
 __version__ = "1.1.0"
diff --git a/tox.ini b/tox.ini
index 84a8e004..1ee185be 100644
--- a/tox.ini
+++ b/tox.ini
@@ -12,9 +12,11 @@ envlist =
 commands =
     py.test test_isort.py {posargs}
 deps =
+    six
     pytest
 
 [testenv:isort-check]
 commands =
     python setup.py isort
 deps =
+    six


Test patch:
diff --git a/test_isort.py b/test_isort.py
index 5b959bed..b4d458ce 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2104,3 +2104,15 @@ def test_sort_within_sections_with_force_to_top_issue_473():
                   'from foo import bar\n')
     assert SortImports(file_contents=test_input, force_sort_within_sections=True,
                        force_to_top=['z']).output == test_input
+
+
+def test_correct_number_of_new_lines_with_comment_issue_435():
+    """Test to ensure that injecting a comment in-between imports doesn't mess up the new line spacing"""
+    test_input = ('import foo\n'
+                  '\n'
+                  '# comment\n'
+                  '\n'
+                  '\n'
+                  'def baz():\n'
+                  '    pass\n')
+    assert SortImports(file_contents=test_input).output == test_input