From 9d12eb5567a1a36afe03da594ea0f94c36efa729 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 17 Jul 2020 00:30:02 -0700 Subject: Fixed issue #1156: Bug related to isort:skip usage followed by a multiline comment block --- CHANGELOG.md | 1 + isort/parse.py | 2 +- tests/test_regressions.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2665354f..006a7295 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. ### 5.1.2 July 17, 2020 - Fixed issue #1219 / #1326: Comments not wrapped for long lines + - Fixed issue #1156: Bug related to isort:skip usage followed by a multiline comment block ### 5.1.1 July 15, 2020 - Fixed issue #1322: Occasionally two extra newlines before comment with `-n` & `--fss`. diff --git a/isort/parse.py b/isort/parse.py index 94b1e316..a67ae1e9 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -371,7 +371,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte categorized_comments["above"]["from"].setdefault(import_from, []).insert( 0, out_lines.pop(-1) ) - if len(out_lines) > max(import_index - 1, 1) - 1: + if len(out_lines): last = out_lines[-1].rstrip() else: last = "" diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 123bec3a..275c8b20 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -402,3 +402,28 @@ from many_stop_words import ( show_diff=True, profile="black", ) + + +def test_comment_blocks_should_stay_associated_without_extra_lines_issue_1156(): + """Tests to ensure isort doesn't add an extra line when there are large import blocks + or otherwise warp the intent. + See: https://github.com/timothycrosley/isort/issues/1156 + """ + assert ( + isort.code( + """from top_level_ignored import config # isort:skip +#################################### +# COMMENT BLOCK SEPARATING THESE # +#################################### +from ast import excepthandler +import logging +""" + ) + == """from top_level_ignored import config # isort:skip +import logging +#################################### +# COMMENT BLOCK SEPARATING THESE # +#################################### +from ast import excepthandler +""" + ) -- cgit v1.2.1