Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dbd1842

Browse files
authoredNov 7, 2021
Merge pull request #1845 from PyCQA/revert-1833-bug/do_not_skip_string
Revert "only skip real comments"
2 parents c154ecf + dcbf92f commit dbd1842

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed
 

‎isort/core.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import output, parse
1010
from .exceptions import FileSkipComment
1111
from .format import format_natural, remove_whitespace
12-
from .settings import FILE_SKIP_RE
12+
from .settings import FILE_SKIP_COMMENTS
1313

1414
CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport")
1515
IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS
@@ -150,11 +150,12 @@ def process(
150150
if stripped_line and not line_separator:
151151
line_separator = line[len(line.rstrip()) :].replace(" ", "").replace("\t", "")
152152

153-
if FILE_SKIP_RE.match(line):
154-
if raise_on_skip:
155-
raise FileSkipComment("Passed in content")
156-
isort_off = True
157-
skip_file = True
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
158159

159160
if not in_quote:
160161
if stripped_line == "# isort: off":

‎isort/settings.py

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

‎tests/unit/test_isort.py

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

841841

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

0 commit comments

Comments
 (0)
Please sign in to comment.