diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2019-03-02 14:58:43 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-02 14:58:43 -0800 |
commit | 7f1c0da72efd378911c72bdaff81bbd36b042b83 (patch) | |
tree | d6c8d6a3825ec265ff511f1fc65db9bb843a4134 | |
parent | 12588b4963e7f46a038e883427e6d54edf12609f (diff) | |
parent | 93a04585381defde79323611c9a8f1cc418db71c (diff) | |
download | isort-7f1c0da72efd378911c72bdaff81bbd36b042b83.tar.gz |
Merge pull request #862 from blueyed/fix-no_lines_before-with-empty-section
Fix no_lines_before with empty section
-rw-r--r-- | isort/isort.py | 22 | ||||
-rw-r--r-- | test_isort.py | 12 |
2 files changed, 27 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() == '': diff --git a/test_isort.py b/test_isort.py index a6bff625..2959c3e0 100644 --- a/test_isort.py +++ b/test_isort.py @@ -2397,6 +2397,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.""" |