Skip to content

Commit 1f093f6

Browse files
authoredOct 30, 2021
Merge pull request #1833 from chrisdecker1201/bug/do_not_skip_string
only skip real comments
2 parents ba81f1c + 3c5da48 commit 1f093f6

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed
 

‎isort/core.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import textwrap
22
from io import StringIO
33
from itertools import chain
4+
from re import match
45
from typing import List, TextIO, Union
56

67
import isort.literal
@@ -9,7 +10,7 @@
910
from . import output, parse
1011
from .exceptions import FileSkipComment
1112
from .format import format_natural, remove_whitespace
12-
from .settings import FILE_SKIP_COMMENTS
13+
from .settings import FILE_SKIP_RE
1314

1415
CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport")
1516
IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS
@@ -150,12 +151,11 @@ def process(
150151
if stripped_line and not line_separator:
151152
line_separator = line[len(line.rstrip()) :].replace(" ", "").replace("\t", "")
152153

153-
for file_skip_comment in FILE_SKIP_COMMENTS:
154-
if file_skip_comment in line:
155-
if raise_on_skip:
156-
raise FileSkipComment("Passed in content")
157-
isort_off = True
158-
skip_file = True
154+
if FILE_SKIP_RE.match(line):
155+
if raise_on_skip:
156+
raise FileSkipComment("Passed in content")
157+
isort_off = True
158+
skip_file = True
159159

160160
if not in_quote:
161161
if stripped_line == "# isort: off":

‎isort/settings.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@
5454
CYTHON_EXTENSIONS = frozenset({"pyx", "pxd"})
5555
SUPPORTED_EXTENSIONS = frozenset({"py", "pyi", *CYTHON_EXTENSIONS})
5656
BLOCKED_EXTENSIONS = frozenset({"pex"})
57-
FILE_SKIP_COMMENTS: Tuple[str, ...] = (
58-
"isort:" + "skip_file",
59-
"isort: " + "skip_file",
60-
) # Concatenated to avoid this file being skipped
57+
FILE_SKIP_RE = re.compile(r"^#?\s?isort:\s?skip_file")
6158
MAX_CONFIG_SEARCH_DEPTH: int = 25 # The number of parent directories to for a config file within
6259
STOP_CONFIG_SEARCH_ON_DIRS: Tuple[str, ...] = (".git", ".hg")
6360
VALID_PY_TARGETS: Tuple[str, ...] = tuple(

‎tests/unit/test_isort.py

+21
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,27 @@ def test_skip_within_file() -> None:
838838
isort.code(test_input, known_third_party=["django"])
839839

840840

841+
def test_skip_comment_without_space_after_hash() -> None:
842+
"""Ensure skipping a whole file works."""
843+
test_input = "#isort: skip_file\nimport django\nimport myproject\n"
844+
with pytest.raises(FileSkipped):
845+
isort.code(test_input, known_third_party=["django"])
846+
847+
848+
def test_skip_comment_with_multiline_comment() -> None:
849+
"""Ensure skipping a whole file works."""
850+
test_input = '"""some comment\n\nisort: skip_file\nimport django\nimport myproject\n"""'
851+
with pytest.raises(FileSkipped):
852+
isort.code(test_input, known_third_party=["django"])
853+
854+
855+
def test_skip_comment_is_no_comment() -> None:
856+
"""Ensure skipping a whole file works."""
857+
test_input = 'content = "# isort:skip_file"'
858+
test_output = isort.code(test_input)
859+
assert test_output == test_input
860+
861+
841862
def test_force_to_top() -> None:
842863
"""Ensure forcing a single import to the top of its category works as expected."""
843864
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n"

0 commit comments

Comments
 (0)
Please sign in to comment.