Skip to content

Commit

Permalink
use regex to mach file_skip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdecker1201 committed Oct 28, 2021
1 parent 4e3f64b commit 3c5da48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
14 changes: 7 additions & 7 deletions isort/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import textwrap
from io import StringIO
from itertools import chain
from re import match
from typing import List, TextIO, Union

import isort.literal
Expand All @@ -9,7 +10,7 @@
from . import output, parse
from .exceptions import FileSkipComment
from .format import format_natural, remove_whitespace
from .settings import FILE_SKIP_COMMENTS
from .settings import FILE_SKIP_RE

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

for file_skip_comment in FILE_SKIP_COMMENTS:
if line.startswith(file_skip_comment):
if raise_on_skip:
raise FileSkipComment("Passed in content")
isort_off = True
skip_file = True
if FILE_SKIP_RE.match(line):
if raise_on_skip:
raise FileSkipComment("Passed in content")
isort_off = True
skip_file = True

if not in_quote:
if stripped_line == "# isort: off":
Expand Down
7 changes: 1 addition & 6 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@
CYTHON_EXTENSIONS = frozenset({"pyx", "pxd"})
SUPPORTED_EXTENSIONS = frozenset({"py", "pyi", *CYTHON_EXTENSIONS})
BLOCKED_EXTENSIONS = frozenset({"pex"})
FILE_SKIP_COMMENTS: Tuple[str, ...] = (
"# isort:" + "skip_file",
"# isort: " + "skip_file",
"#isort:" + "skip_file",
"#isort: " + "skip_file",
) # Concatenated to avoid this file being skipped
FILE_SKIP_RE = re.compile(r"^#?\s?isort:\s?skip_file")
MAX_CONFIG_SEARCH_DEPTH: int = 25 # The number of parent directories to for a config file within
STOP_CONFIG_SEARCH_ON_DIRS: Tuple[str, ...] = (".git", ".hg")
VALID_PY_TARGETS: Tuple[str, ...] = tuple(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,13 @@ def test_skip_comment_without_space_after_hash() -> None:
isort.code(test_input, known_third_party=["django"])


def test_skip_comment_with_multiline_comment() -> None:
"""Ensure skipping a whole file works."""
test_input = '"""some comment\n\nisort: skip_file\nimport django\nimport myproject\n"""'
with pytest.raises(FileSkipped):
isort.code(test_input, known_third_party=["django"])


def test_skip_comment_is_no_comment() -> None:
"""Ensure skipping a whole file works."""
test_input = 'content = "# isort:skip_file"'
Expand Down

0 comments on commit 3c5da48

Please sign in to comment.