Skip to content

Commit

Permalink
Fixed #1398: isort: off comment doesn't work, if it's the top comment…
Browse files Browse the repository at this point in the history
… in the file.
  • Loading branch information
timothycrosley committed Aug 22, 2020
1 parent 66bc89f commit 9a6530c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
7 changes: 3 additions & 4 deletions isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def process(
if file_skip_comment in line:
raise FileSkipComment("Passed in content")

if not in_quote and stripped_line == "# isort: off":
isort_off = True

if (
(index == 0 or (index in (1, 2) and not contains_imports))
and stripped_line.startswith("#")
Expand Down Expand Up @@ -175,13 +178,9 @@ def process(

not_imports = bool(in_quote) or in_top_comment or isort_off
if not (in_quote or in_top_comment):
stripped_line = line.strip()
if isort_off:
if stripped_line == "# isort: on":
isort_off = False
elif stripped_line == "# isort: off":
not_imports = True
isort_off = True
elif stripped_line.endswith("# isort: split"):
not_imports = True
elif stripped_line in CODE_SORT_COMMENTS:
Expand Down
47 changes: 47 additions & 0 deletions tests/test_action_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Tests for isort action comments, such as isort: skip"""
import isort


def test_isort_off_and_on():
"""Test so ensure isort: off action comment and associated on action comment work together"""

# as top of file comment
assert (
isort.code(
"""# isort: off
import a
import a
# isort: on
import a
import a
"""
)
== """# isort: off
import a
import a
# isort: on
import a
"""
)
# as middle comment
assert (
isort.code(
"""
import a
import a
# isort: off
import a
import a
"""
)
== """
import a
# isort: off
import a
import a
"""
)

0 comments on commit 9a6530c

Please sign in to comment.