diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dcbab2ba..f7051ed32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. - Fixed #1297: Usage of `--add-imports` alongside `--check` is broken. - Fixed #1289: Stream usage no longer auto picking up config file from current working directory. - Fixed #1296: Force_single_line setting removes immediately following comment line. + - Fixed #1295: `ensure_newline_before_comments` doesnt work with `force_sort_within_sections`. ### 5.0.4 July 6, 2020 - Fixed #1264: a regression with comment handling and `force_sort_within_sections` config option diff --git a/isort/output.py b/isort/output.py index 95aa9ddd5..9e41f64c5 100644 --- a/isort/output.py +++ b/isort/output.py @@ -130,7 +130,11 @@ def sorted_imports( # uncollapse comments section_output = [] for line in new_section_output: - section_output.extend(getattr(line, "comments", ())) + comments = getattr(line, "comments", ()) + if comments: + if new_section_output and config.ensure_newline_before_comments: + section_output.append("") + section_output.extend(comments) section_output.append(str(line)) section_name = section diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 70ef61962..69494f307 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -166,3 +166,20 @@ def test_force_single_line_shouldnt_remove_preceding_comment_lines_issue_1296(): """ # assert isort.code(test_input) == test_input assert isort.code(test_input, force_single_line=True) == test_input + + +def test_ensure_new_line_before_comments_mixed_with_ensure_newline_before_comments_1295(): + """Tests to ensure that the black profile can be used in conjunction with + force_sort_within_sections. + + See: https://github.com/timothycrosley/isort/issues/1295 + """ + test_input = """ +from openzwave.group import ZWaveGroup +from openzwave.network import ZWaveNetwork + +# pylint: disable=import-error +from openzwave.option import ZWaveOption +""" + assert isort.code(test_input, profile="black") == test_input + assert isort.code(test_input, profile="black", force_sort_within_sections=True) == test_input