summaryrefslogtreecommitdiff
path: root/isort
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2019-03-02 14:58:43 -0800
committerGitHub <noreply@github.com>2019-03-02 14:58:43 -0800
commit7f1c0da72efd378911c72bdaff81bbd36b042b83 (patch)
treed6c8d6a3825ec265ff511f1fc65db9bb843a4134 /isort
parent12588b4963e7f46a038e883427e6d54edf12609f (diff)
parent93a04585381defde79323611c9a8f1cc418db71c (diff)
downloadisort-7f1c0da72efd378911c72bdaff81bbd36b042b83.tar.gz
Merge pull request #862 from blueyed/fix-no_lines_before-with-empty-section
Fix no_lines_before with empty section
Diffstat (limited to 'isort')
-rw-r--r--isort/isort.py22
1 files changed, 15 insertions, 7 deletions
diff --git a/isort/isort.py b/isort/isort.py
index a763c92c..44a1c9e3 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -555,7 +555,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))
@@ -588,8 +588,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
@@ -599,11 +602,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() == '':