From 16d933fcf5ecd318bdb4b5574ac5ad8d4c88a99f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 7 Jan 2023 17:30:18 -0500 Subject: [PATCH] Respect isort:skip action comment (#1722) Resolves: #1718. --- resources/test/fixtures/isort/skip.py | 30 ++++++++++++------- src/directives.rs | 7 +++-- .../ruff__isort__tests__skip.py.snap | 20 ++++++------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/resources/test/fixtures/isort/skip.py b/resources/test/fixtures/isort/skip.py index 02332bf21d43c..dbc836c64317a 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 - -import sys -import os # isort: skip -import collections -import abc +def f(): + # isort: off + import sys + import os + import collections + # isort: on + + +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 27c9eef777f87..e6fec89bd880b 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 0be98db662ed1..569e42ca0901d 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: ~