Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only skip real comments #1833

Merged
merged 4 commits into from Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion isort/core.py
Expand Up @@ -151,7 +151,7 @@ def process(
line_separator = line[len(line.rstrip()) :].replace(" ", "").replace("\t", "")

for file_skip_comment in FILE_SKIP_COMMENTS:
if file_skip_comment in line:
if line.startswith(file_skip_comment):
if raise_on_skip:
raise FileSkipComment("Passed in content")
isort_off = True
Expand Down
6 changes: 4 additions & 2 deletions isort/settings.py
Expand Up @@ -55,8 +55,10 @@
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",
"#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")
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_isort.py
Expand Up @@ -837,14 +837,24 @@ def test_skip_within_file() -> None:
with pytest.raises(FileSkipped):
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_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"
test_output = isort.code(test_input, force_to_top=["lib5"])
assert test_output == "import lib5\nimport lib1\nimport lib2\nimport lib6\n"


def test_add_imports() -> None:
"""Ensures adding imports works as expected."""
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n\n"
Expand Down