Respect isort:skip action comment (#1722)

Resolves: #1718.
This commit is contained in:
Charlie Marsh 2023-01-07 17:30:18 -05:00 committed by GitHub
parent a9cc56b2ac
commit 16d933fcf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 21 deletions

View File

@ -1,9 +1,19 @@
def f():
# isort: off # isort: off
import sys import sys
import os import os
import collections import collections
# isort: on # isort: on
def f():
import sys
import os # isort: skip
import collections
import abc
def f():
import sys import sys
import os # isort:skip import os # isort:skip
import collections import collections

View File

@ -104,10 +104,13 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
continue; 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(); let comment_text = comment_text.trim_end();
if comment_text == "# isort: split" { if comment_text == "# isort: split" {
splits.push(start.row()); 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; skip_file = true;
} else if off.is_some() { } else if off.is_some() {
if comment_text == "# isort: on" { if comment_text == "# isort: on" {
@ -119,7 +122,7 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
off = None; off = None;
} }
} else { } else {
if comment_text.contains("isort: skip") { if comment_text.contains("isort: skip") || comment_text.contains("isort:skip") {
exclusions.insert(start.row()); exclusions.insert(start.row());
} else if comment_text == "# isort: off" { } else if comment_text == "# isort: off" {
off = Some(start); off = Some(start);

View File

@ -5,35 +5,35 @@ expression: checks
- kind: - kind:
UnsortedImports: ~ UnsortedImports: ~
location: location:
row: 7 row: 12
column: 0 column: 0
end_location: end_location:
row: 8 row: 14
column: 0 column: 0
fix: fix:
content: "import sys\n\n" content: " import abc\n import collections\n"
location: location:
row: 7 row: 12
column: 0 column: 0
end_location: end_location:
row: 8 row: 14
column: 0 column: 0
parent: ~ parent: ~
- kind: - kind:
UnsortedImports: ~ UnsortedImports: ~
location: location:
row: 9 row: 19
column: 0 column: 0
end_location: end_location:
row: 11 row: 21
column: 0 column: 0
fix: fix:
content: " import abc\n import collections\n" content: " import abc\n import collections\n"
location: location:
row: 9 row: 19
column: 0 column: 0
end_location: end_location:
row: 11 row: 21
column: 0 column: 0
parent: ~ parent: ~