Skip to content

Commit

Permalink
Allow # ruff: prefix for isort action comments (#3493)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 14, 2023
1 parent c50d6da commit 432059d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 21 deletions.
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/isort/ruff_skip_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ruff: isort: skip_file
import e
import f

# isort: split

import a
import b
import c
import d
8 changes: 8 additions & 0 deletions crates/ruff/resources/test/fixtures/isort/skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def f():
# isort: on


def f():
# ruff: isort: off
import sys
import os
import collections
# ruff: isort: on


def f():
import sys
import os # isort: skip
Expand Down
14 changes: 10 additions & 4 deletions crates/ruff/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
// 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" {
if matches!(comment_text, "# isort: split" | "# ruff: isort: split") {
splits.push(start.row());
} else if comment_text == "# isort: skip_file" || comment_text == "# isort:skip_file" {
} else if matches!(
comment_text,
"# isort: skip_file"
| "# isort:skip_file"
| "# ruff: isort: skip_file"
| "# ruff: isort:skip_file"
) {
return IsortDirectives {
skip_file: true,
..IsortDirectives::default()
};
} else if off.is_some() {
if comment_text == "# isort: on" {
if comment_text == "# isort: on" || comment_text == "# ruff: isort: on" {
if let Some(start) = off {
for row in start.row() + 1..=end.row() {
exclusions.insert(row);
Expand All @@ -126,7 +132,7 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
} else {
if comment_text.contains("isort: skip") || comment_text.contains("isort:skip") {
exclusions.insert(start.row());
} else if comment_text == "# isort: off" {
} else if comment_text == "# isort: off" || comment_text == "# ruff: isort: off" {
off = Some(start);
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ mod tests {
#[test_case(Path::new("inline_comments.py"))]
#[test_case(Path::new("insert_empty_lines.py"))]
#[test_case(Path::new("insert_empty_lines.pyi"))]
#[test_case(Path::new("isort_skip_file.py"))]
#[test_case(Path::new("leading_prefix.py"))]
#[test_case(Path::new("magic_trailing_comma.py"))]
#[test_case(Path::new("natural_order.py"))]
Expand All @@ -391,12 +392,12 @@ mod tests {
#[test_case(Path::new("preserve_tabs_2.py"))]
#[test_case(Path::new("relative_imports_order.py"))]
#[test_case(Path::new("reorder_within_section.py"))]
#[test_case(Path::new("ruff_skip_file.py"))]
#[test_case(Path::new("separate_first_party_imports.py"))]
#[test_case(Path::new("separate_future_imports.py"))]
#[test_case(Path::new("separate_local_folder_imports.py"))]
#[test_case(Path::new("separate_third_party_imports.py"))]
#[test_case(Path::new("skip.py"))]
#[test_case(Path::new("skip_file.py"))]
#[test_case(Path::new("sort_similar_imports.py"))]
#[test_case(Path::new("split.py"))]
#[test_case(Path::new("star_before_others.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/isort/mod.rs
expression: diagnostics
---
[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/isort/mod.rs
expression: diagnostics
---
[]

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ expression: diagnostics
suggestion: Organize imports
fixable: true
location:
row: 12
row: 20
column: 0
end_location:
row: 14
row: 22
column: 0
fix:
content: " import abc\n import collections\n"
location:
row: 12
row: 20
column: 0
end_location:
row: 14
row: 22
column: 0
parent: ~
- kind:
Expand All @@ -28,18 +28,18 @@ expression: diagnostics
suggestion: Organize imports
fixable: true
location:
row: 19
row: 27
column: 0
end_location:
row: 21
row: 29
column: 0
fix:
content: " import abc\n import collections\n"
location:
row: 19
row: 27
column: 0
end_location:
row: 21
row: 29
column: 0
parent: ~

This file was deleted.

9 changes: 7 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,17 @@ automatically add `noqa` directives to all failing lines, with the appropriate r

### Action comments

Ruff respects `isort`'s [action comments](https://pycqa.github.io/isort/docs/configuration/action_comments.html)
Ruff respects isort's [action comments](https://pycqa.github.io/isort/docs/configuration/action_comments.html)
(`# isort: skip_file`, `# isort: on`, `# isort: off`, `# isort: skip`, and `# isort: split`), which
enable selectively enabling and disabling import sorting for blocks of code and other inline
configuration.

See the [`isort` documentation](https://pycqa.github.io/isort/docs/configuration/action_comments.html)
Ruff will also respect variants of these action comments with a `# ruff:` prefix
(e.g., `# ruff: isort: skip_file`, `# ruff: isort: on`, and so on). These variants more clearly
convey that the action comment is intended for Ruff, but are functionally equivalent to the
isort variants.

See the [isort documentation](https://pycqa.github.io/isort/docs/configuration/action_comments.html)
for more.

## Exit codes
Expand Down

0 comments on commit 432059d

Please sign in to comment.