summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-07-17 00:30:02 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2020-07-17 00:30:02 -0700
commit9d12eb5567a1a36afe03da594ea0f94c36efa729 (patch)
tree846d042a8965b81f527b52c2a4171ac6409e3cee
parent4af5a6f73db4af34e9bdc1f752f0800c65c516dc (diff)
downloadisort-9d12eb5567a1a36afe03da594ea0f94c36efa729.tar.gz
Fixed issue #1156: Bug related to isort:skip usage followed by a multiline comment block
-rw-r--r--CHANGELOG.md1
-rw-r--r--isort/parse.py2
-rw-r--r--tests/test_regressions.py25
3 files changed, 27 insertions, 1 deletions
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
+"""
+ )