diff --git a/resources/test/fixtures/isort/skip.py b/resources/test/fixtures/isort/skip.py index 02332bf21d..dbc836c643 100644 --- a/resources/test/fixtures/isort/skip.py +++ b/resources/test/fixtures/isort/skip.py @@ -1,10 +1,20 @@ -# isort: off -import sys -import os -import collections -# isort: on +def f(): + # isort: off + import sys + import os + import collections + # isort: on -import sys -import os # isort: skip -import collections -import abc + +def f(): + import sys + import os # isort: skip + import collections + import abc + + +def f(): + import sys + import os # isort:skip + import collections + import abc diff --git a/src/directives.rs b/src/directives.rs index 27c9eef777..e6fec89bd8 100644 --- a/src/directives.rs +++ b/src/directives.rs @@ -104,10 +104,13 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives { continue; }; + // `isort` allows for `# isort: skip` and `# isort: skip_file` to include or + // omit a space after the colon. The remaining action comments are + // required to include the space, and must appear on their own lines. let comment_text = comment_text.trim_end(); if comment_text == "# isort: split" { splits.push(start.row()); - } else if comment_text == "# isort: skip_file" { + } else if comment_text == "# isort: skip_file" || comment_text == "# isort:skip_file" { skip_file = true; } else if off.is_some() { if comment_text == "# isort: on" { @@ -119,7 +122,7 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives { off = None; } } else { - if comment_text.contains("isort: skip") { + if comment_text.contains("isort: skip") || comment_text.contains("isort:skip") { exclusions.insert(start.row()); } else if comment_text == "# isort: off" { off = Some(start); diff --git a/src/isort/snapshots/ruff__isort__tests__skip.py.snap b/src/isort/snapshots/ruff__isort__tests__skip.py.snap index 0be98db662..569e42ca09 100644 --- a/src/isort/snapshots/ruff__isort__tests__skip.py.snap +++ b/src/isort/snapshots/ruff__isort__tests__skip.py.snap @@ -5,35 +5,35 @@ expression: checks - kind: UnsortedImports: ~ location: - row: 7 + row: 12 column: 0 end_location: - row: 8 + row: 14 column: 0 fix: - content: "import sys\n\n" + content: " import abc\n import collections\n" location: - row: 7 + row: 12 column: 0 end_location: - row: 8 + row: 14 column: 0 parent: ~ - kind: UnsortedImports: ~ location: - row: 9 + row: 19 column: 0 end_location: - row: 11 + row: 21 column: 0 fix: - content: "import abc\nimport collections\n" + content: " import abc\n import collections\n" location: - row: 9 + row: 19 column: 0 end_location: - row: 11 + row: 21 column: 0 parent: ~