Skip to content

Commit

Permalink
Infer indentation with imports when logical indent is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 30, 2024
1 parent 921bc15 commit af2a95e
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 478 deletions.
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/isort/two_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# If the file doesn't contain a logical indent token, we should still detect two-space indentation on imports.
from math import (
sin,
tan,
cos,
nan,
pi,
)

del sin, cos, tan, pi, nan
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

print("brace {} %s" % (1,))

print((
"foo %s "
"bar %s" % (x, y)
))

print(
"%s" % (
"trailing comma",
Expand All @@ -52,10 +57,6 @@

print("%(a)s" % {"a" : 1})

print((
"foo %s "
"bar %s" % (x, y)
))

print(
"foo %(foo)s "
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ mod tests {
#[test_case(Path::new("split.py"))]
#[test_case(Path::new("star_before_others.py"))]
#[test_case(Path::new("trailing_suffix.py"))]
#[test_case(Path::new("two_space.py"))]
#[test_case(Path::new("type_comments.py"))]
#[test_case(Path::new("unicode.py"))]
fn default(path: &Path) -> Result<()> {
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/isort/rules/organize_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub(crate) fn organize_imports(
return None;
}

println!("Expected: {expected}");

let mut diagnostic = Diagnostic::new(UnsortedImports, range);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
indent(&expected, indentation).to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
two_space.py:2:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | # If the file doesn't contain a logical indent token, we should still detect two-space indentation on imports.
2 | / from math import (
3 | | sin,
4 | | tan,
5 | | cos,
6 | | nan,
7 | | pi,
8 | | )
9 | |
10 | | del sin, cos, tan, pi, nan
| |_^ I001
|
= help: Organize imports

Safe fix
1 1 | # If the file doesn't contain a logical indent token, we should still detect two-space indentation on imports.
2 2 | from math import (
3 |- sin,
4 |- tan,
5 3 | cos,
6 4 | nan,
7 5 | pi,
6 |+ sin,
7 |+ tan,
8 8 | )
9 9 |
10 10 | del sin, cos, tan, pi, nan
Loading

0 comments on commit af2a95e

Please sign in to comment.