Skip to content

Commit 3c5da48

Browse files
committedOct 28, 2021
use regex to mach file_skip
1 parent 4e3f64b commit 3c5da48

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
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 line.startswith(file_skip_comment):
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-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +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-
"#isort:" + "skip_file",
61-
"#isort: " + "skip_file",
62-
) # Concatenated to avoid this file being skipped
57+
FILE_SKIP_RE = re.compile(r"^#?\s?isort:\s?skip_file")
6358
MAX_CONFIG_SEARCH_DEPTH: int = 25 # The number of parent directories to for a config file within
6459
STOP_CONFIG_SEARCH_ON_DIRS: Tuple[str, ...] = (".git", ".hg")
6560
VALID_PY_TARGETS: Tuple[str, ...] = tuple(

‎tests/unit/test_isort.py

+7
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,13 @@ def test_skip_comment_without_space_after_hash() -> None:
845845
isort.code(test_input, known_third_party=["django"])
846846

847847

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+
848855
def test_skip_comment_is_no_comment() -> None:
849856
"""Ensure skipping a whole file works."""
850857
test_input = 'content = "# isort:skip_file"'

0 commit comments

Comments
 (0)
Please sign in to comment.