From dcbf92fbaaf1a971e96746c47eb91e35e7badc07 Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Sat, 6 Nov 2021 22:59:42 -0700 Subject: [PATCH] Revert "only skip real comments" --- isort/core.py | 13 +++++++------ isort/settings.py | 5 ++++- tests/unit/test_isort.py | 21 --------------------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/isort/core.py b/isort/core.py index 78e0a1c03..c0bcd363a 100644 --- a/isort/core.py +++ b/isort/core.py @@ -9,7 +9,7 @@ from . import output, parse from .exceptions import FileSkipComment from .format import format_natural, remove_whitespace -from .settings import FILE_SKIP_RE +from .settings import FILE_SKIP_COMMENTS CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport") IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS @@ -150,11 +150,12 @@ def process( if stripped_line and not line_separator: line_separator = line[len(line.rstrip()) :].replace(" ", "").replace("\t", "") - if FILE_SKIP_RE.match(line): - if raise_on_skip: - raise FileSkipComment("Passed in content") - isort_off = True - skip_file = True + for file_skip_comment in FILE_SKIP_COMMENTS: + if file_skip_comment in 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": diff --git a/isort/settings.py b/isort/settings.py index 29e17bcb7..d0060d056 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -54,7 +54,10 @@ CYTHON_EXTENSIONS = frozenset({"pyx", "pxd"}) SUPPORTED_EXTENSIONS = frozenset({"py", "pyi", *CYTHON_EXTENSIONS}) BLOCKED_EXTENSIONS = frozenset({"pex"}) -FILE_SKIP_RE = re.compile(r"^#?\s?isort:\s?skip_file") +FILE_SKIP_COMMENTS: Tuple[str, ...] = ( + "isort:" + "skip_file", + "isort: " + "skip_file", +) # Concatenated to avoid this file being skipped 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( diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 6f885581c..8a224ec8d 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -839,27 +839,6 @@ def test_skip_within_file() -> None: isort.code(test_input, known_third_party=["django"]) -def test_skip_comment_without_space_after_hash() -> None: - """Ensure skipping a whole file works.""" - test_input = "#isort: skip_file\nimport django\nimport myproject\n" - with pytest.raises(FileSkipped): - 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"' - test_output = isort.code(test_input) - assert test_output == test_input - - def test_force_to_top() -> None: """Ensure forcing a single import to the top of its category works as expected.""" test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n"