diff --git a/isort/core.py b/isort/core.py index 010aa7f6b..35738abd9 100644 --- a/isort/core.py +++ b/isort/core.py @@ -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("#") @@ -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: diff --git a/tests/test_action_comments.py b/tests/test_action_comments.py new file mode 100644 index 000000000..508db0d2e --- /dev/null +++ b/tests/test_action_comments.py @@ -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 +""" + )