Skip to content

Commit

Permalink
[isort] Omit trailing whitespace in unsorted-imports (I001) (#1…
Browse files Browse the repository at this point in the history
…5518)

## Summary
The fix range for sorting imports accounts for trailing whitespace, but
we should only show the trimmed range to the user when displaying the
diagnostic. So this PR changes the diagnostic range.

Closes #15504 

## Test Plan

Reviewed snapshot changes
  • Loading branch information
dylwil3 authored Jan 18, 2025
1 parent 001e5ad commit 8a50f3f
Show file tree
Hide file tree
Showing 102 changed files with 242 additions and 375 deletions.
7 changes: 3 additions & 4 deletions crates/ruff_linter/src/rules/isort/rules/organize_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ pub(crate) fn organize_imports(
);

// Expand the span the entire range, including leading and trailing space.
let range = TextRange::new(locator.line_start(range.start()), trailing_line_end);
let actual = locator.slice(range);
let fix_range = TextRange::new(locator.line_start(range.start()), trailing_line_end);
let actual = locator.slice(fix_range);
if matches_ignoring_indentation(actual, &expected) {
return None;
}

let mut diagnostic = Diagnostic::new(UnsortedImports, range);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
indent(&expected, indentation).to_string(),
range,
fix_range,
)));
Some(diagnostic)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ separate_subpackage_first_and_third_party_imports.py:1:1: I001 [*] Import block
6 | | import foo
7 | | import foo.bar
8 | | import foo.bar.baz
| |___________________^ I001
| |__________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ separate_subpackage_first_and_third_party_imports.py:1:1: I001 [*] Import block
6 | | import foo
7 | | import foo.bar
8 | | import foo.bar.baz
| |___________________^ I001
| |__________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_newline_before_comments.py:1:1: I001 [*] Import block is un-sorted or un-for
5 | | # This is a comment, but it starts a new section, so we don't need to add a newline
6 | | # before it.
7 | | import leading_prefix
| |______________________^ I001
| |_____________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ as_imports_comments.py:1:1: I001 [*] Import block is un-sorted or un-formatted
13 | | from bop import ( # Comment on `bop`
14 | | Member # Comment on `Member`
15 | | )
| |__^ I001
| |_^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bom_unsorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import foo
2 | | import bar
| |___________^ I001
| |__________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ case_sensitive.py:1:1: I001 [*] Import block is un-sorted or un-formatted
7 | | import f
8 | | from g import a, B, c
9 | | from h import A, b, C
| |______________________^ I001
| |_____________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ relative_imports_order.py:1:1: I001 [*] Import block is un-sorted or un-formatte
1 | / from ... import a
2 | | from .. import b
3 | | from . import c
| |________________^ I001
| |_______________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ combine_as_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
2 | | from module import CONSTANT
3 | | from module import function
4 | | from module import function as f
| |_________________________________^ I001
| |________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ combine_as_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
2 | | from module import CONSTANT
3 | | from module import function
4 | | from module import function as f
| |_________________________________^ I001
| |________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ combine_import_from.py:1:1: I001 [*] Import block is un-sorted or un-formatted
3 | | from collections import Collection
4 | | from collections import ChainMap
5 | | from collections import MutableSequence, MutableMapping
| |________________________________________________________^ I001
| |_______________________________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ comments.py:3:1: I001 [*] Import block is un-sorted or un-formatted
31 | |
32 | | from F import a # Comment 1
33 | | from F import b
| |________________^ I001
| |_______________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deduplicate_imports.py:1:1: I001 [*] Import block is un-sorted or un-formatted
2 | | import os
3 | | import os as os1
4 | | import os as os2
| |_________________^ I001
| |________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bar.py:1:1: I001 [*] Import block is un-sorted or un-formatted
1 | / import os
2 | | import pandas
3 | | import foo.baz
| |_______________^ I001
| |______________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
fit_line_length.py:7:1: I001 [*] Import block is un-sorted or un-formatted
fit_line_length.py:7:5: I001 [*] Import block is un-sorted or un-formatted
|
6 | if indented:
7 | / from line_with_88 import aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Expand All @@ -12,7 +12,7 @@ fit_line_length.py:7:1: I001 [*] Import block is un-sorted or un-formatted
12 | | from line_with_93 import (
13 | | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
14 | | )
| |______^ I001
| |_____^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fit_line_length_comment.py:1:1: I001 [*] Import block is un-sorted or un-formatt
6 | | from f import g # 012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ
7 | | # The next import doesn't fit on one line.
8 | | from h import i # 012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9012ß9💣2ℝ9
| |__________________________________________________________________________________________^ I001
| |_______________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ force_single_line.py:1:1: I001 [*] Import block is un-sorted or un-formatted
25 | |
26 | | # comment 9
27 | | from baz import * # comment 10
| |________________________________^ I001
| |_________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ force_sort_within_sections.py:1:1: I001 [*] Import block is un-sorted or un-form
11 | | from . import my
12 | | from .my.nested import fn2
13 | | from ...grandparent import fn3
| |_______________________________^ I001
| |______________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ force_sort_within_sections.py:1:1: I001 [*] Import block is un-sorted or un-form
11 | | from . import my
12 | | from .my.nested import fn2
13 | | from ...grandparent import fn3
| |_______________________________^ I001
| |______________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ force_sort_within_sections_future.py:1:1: I001 [*] Import block is un-sorted or
|
1 | / import __future__
2 | | from __future__ import annotations
| |___________________________________^ I001
| |__________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ force_sort_within_sections_with_as_names.py:1:1: I001 [*] Import block is un-sor
3 | | from datetime import timedelta
4 | | import datetime as dt
5 | | import datetime
| |________________^ I001
| |_______________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ force_to_top.py:1:1: I001 [*] Import block is un-sorted or un-formatted
21 | | import lib3.lib4.lib5
22 | | from lib3.lib4 import foo
23 | | from lib3.lib4.lib5 import foo
| |_______________________________^ I001
| |______________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ force_to_top.py:1:1: I001 [*] Import block is un-sorted or un-formatted
21 | | import lib3.lib4.lib5
22 | | from lib3.lib4 import foo
23 | | from lib3.lib4.lib5 import foo
| |_______________________________^ I001
| |______________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ force_wrap_aliases.py:1:1: I001 [*] Import block is un-sorted or un-formatted
1 | / from .a import a1 as a1, a2 as a2
2 | | from .b import b1 as b1
3 | | from .c import c1
| |__________________^ I001
| |_________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ force_wrap_aliases.py:1:1: I001 [*] Import block is un-sorted or un-formatted
1 | / from .a import a1 as a1, a2 as a2
2 | | from .b import b1 as b1
3 | | from .c import c1
| |__________________^ I001
| |_________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ forced_separate.py:3:1: I001 [*] Import block is un-sorted or un-formatted
6 | | from experiments.starry import *
7 | | from experiments.weird import varieties
8 | | from office_helper.assistants import entity_registry as er
| |___________________________________________________________^ I001
| |__________________________________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ future_from.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import __future__
2 | | from __future__ import annotations
| |___________________________________^ I001
| |__________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ separate_subpackage_first_and_third_party_imports.py:1:1: I001 [*] Import block
6 | | import foo
7 | | import foo.bar
8 | | import foo.bar.baz
| |___________________^ I001
| |__________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
if_elif_else.py:6:1: I001 [*] Import block is un-sorted or un-formatted
if_elif_else.py:6:5: I001 [*] Import block is un-sorted or un-formatted
|
4 | from setuptools.command.sdist import sdist as _sdist
5 | else:
6 | / from setuptools.command.sdist import sdist as _sdist
7 | | from distutils.command.sdist import sdist as _sdist
| |________________________________________________________^ I001
| |_______________________________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import_from_after_import.py:1:1: I001 [*] Import block is un-sorted or un-format
|
1 | / from collections import Collection
2 | | import os
| |__________^ I001
| |_________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ inline_comments.py:1:1: I001 [*] Import block is un-sorted or un-formatted
9 | | from c.prometheus.metrics import TERMINAL_CURRENTLY_RUNNING_TOTAL # type:ignore[attr-defined]
10 | |
11 | | from d.prometheus.metrics import TERMINAL_CURRENTLY_RUNNING_TOTAL, OTHER_RUNNING_TOTAL # type:ignore[attr-defined]
| |____________________________________________________________________________________________________________________^ I001
| |______________________________________________________________________________________^ I001
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ insert_empty_lines.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import a
2 | | import b
| |_________^ I001
| |________^ I001
3 | x = 1
4 | import os
5 | import sys
|
= help: Organize imports

Expand All @@ -26,10 +25,9 @@ insert_empty_lines.py:4:1: I001 [*] Import block is un-sorted or un-formatted
3 | x = 1
4 | / import os
5 | | import sys
| |___________^ I001
| |__________^ I001
6 | def f():
7 | pass
8 | if True:
|
= help: Organize imports

Expand All @@ -49,10 +47,8 @@ insert_empty_lines.py:14:1: I001 [*] Import block is un-sorted or un-formatted
13 | y = 1
14 | / import os
15 | | import sys
| |___________^ I001
| |__________^ I001
16 | """Docstring"""
17 |
18 | if True:
|
= help: Organize imports

Expand All @@ -67,12 +63,10 @@ insert_empty_lines.py:14:1: I001 [*] Import block is un-sorted or un-formatted

insert_empty_lines.py:52:1: I001 [*] Import block is un-sorted or un-formatted
|
52 | / import os
53 | |
| |__^ I001
54 | # Comment goes here.
55 | def f():
56 | pass
52 | import os
| ^^^^^^^^^ I001
53 |
54 | # Comment goes here.
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ insert_empty_lines.pyi:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import a
2 | | import b
| |_________^ I001
| |________^ I001
3 | x = 1
4 | import os
5 | import sys
|
= help: Organize imports

Expand All @@ -26,10 +25,9 @@ insert_empty_lines.pyi:4:1: I001 [*] Import block is un-sorted or un-formatted
3 | x = 1
4 | / import os
5 | | import sys
| |___________^ I001
| |__________^ I001
6 | def f():
7 | pass
8 | if True:
|
= help: Organize imports

Expand All @@ -48,10 +46,8 @@ insert_empty_lines.pyi:14:1: I001 [*] Import block is un-sorted or un-formatted
13 | y = 1
14 | / import os
15 | | import sys
| |___________^ I001
| |__________^ I001
16 | """Docstring"""
17 |
18 | if True:
|
= help: Organize imports

Expand Down
Loading

0 comments on commit 8a50f3f

Please sign in to comment.