Repository: timothycrosley/isort
Pull request #862 (2019-03-01 11:18:04)

Fix no_lines_before with empty section
This fixes "no_lines_before" to also be respected from previous empty
sections.


Patch:
diff --git a/isort/isort.py b/isort/isort.py
index ca28d56d..270f984d 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -558,7 +558,7 @@ class SortImports(object):
             sections = ('no_sections', )
 
         output = []  # type: List[str]
-        prev_section_has_imports = False
+        pending_lines_before = False
         for section in sections:
             straight_modules = self.imports[section]['straight']
             straight_modules = nsorted(straight_modules, key=lambda key: self._module_key(key, self.config, section_name=section))
@@ -591,8 +591,11 @@ class SortImports(object):
                         line = line.lower()
                     return '{0}{1}'.format(section, line)
                 section_output = nsorted(section_output, key=by_module)
+
+            section_name = section
+            no_lines_before = section_name in self.config['no_lines_before']
+
             if section_output:
-                section_name = section
                 if section_name in self.place_imports:
                     self.place_imports[section_name] = section_output
                     continue
@@ -602,11 +605,16 @@ class SortImports(object):
                     section_comment = "# {0}".format(section_title)
                     if section_comment not in self.out_lines[0:1] and section_comment not in self.in_lines[0:1]:
                         section_output.insert(0, section_comment)
-                if prev_section_has_imports and section_name in self.config['no_lines_before']:
-                    while output and output[-1].strip() == '':
-                        output.pop()
-                output += section_output + ([''] * self.config['lines_between_sections'])
-            prev_section_has_imports = bool(section_output)
+
+                if pending_lines_before or not no_lines_before:
+                    output += ([''] * self.config['lines_between_sections'])
+
+                output += section_output
+
+                pending_lines_before = False
+            else:
+                pending_lines_before = pending_lines_before or not no_lines_before
+
         while output and output[-1].strip() == '':
             output.pop()
         while output and output[0].strip() == '':


Test patch:
diff --git a/test_isort.py b/test_isort.py
index b35d6cdd..412f3f31 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2394,6 +2394,18 @@ def test_not_splitted_sections():
     assert SortImports(file_contents=test_input, no_lines_before=['STDLIB']).output == test_input
 
 
+def test_no_lines_before_empty_section():
+    test_input = ('import first\n'
+                  'import custom\n')
+    assert SortImports(
+        file_contents=test_input,
+        known_third_party=["first"],
+        known_custom=["custom"],
+        sections=['THIRDPARTY', 'LOCALFOLDER', 'CUSTOM'],
+        no_lines_before=['THIRDPARTY', 'LOCALFOLDER', 'CUSTOM'],
+    ).output == test_input
+
+
 def test_no_inline_sort():
     """Test to ensure multiple `from` imports in one line are not sorted if `--no-inline-sort` flag
     is enabled. If `--force-single-line-imports` flag is enabled, then `--no-inline-sort` is ignored."""