mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
Fix multi-segment import removal (#480)
This commit is contained in:
parent
16c2e3a995
commit
c00bd489f1
6 changed files with 93 additions and 29 deletions
2
resources/test/fixtures/F401_0.py
vendored
2
resources/test/fixtures/F401_0.py
vendored
|
@ -86,5 +86,3 @@ else:
|
|||
|
||||
|
||||
CustomInt: TypeAlias = "np.int8 | np.int16"
|
||||
|
||||
from foo.bar import baz
|
||||
|
|
5
resources/test/fixtures/F401_5.py
vendored
Normal file
5
resources/test/fixtures/F401_5.py
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
"""Test: removal of multi-segment and aliases imports."""
|
||||
from a.b import c
|
||||
from d.e import f as g
|
||||
import h.i
|
||||
import j.k as l
|
|
@ -330,6 +330,7 @@ mod tests {
|
|||
#[test_case(CheckCode::F401, Path::new("F401_2.py"); "F401_2")]
|
||||
#[test_case(CheckCode::F401, Path::new("F401_3.py"); "F401_3")]
|
||||
#[test_case(CheckCode::F401, Path::new("F401_4.py"); "F401_4")]
|
||||
#[test_case(CheckCode::F401, Path::new("F401_5.py"); "F401_5")]
|
||||
#[test_case(CheckCode::F402, Path::new("F402.py"); "F402")]
|
||||
#[test_case(CheckCode::F403, Path::new("F403.py"); "F403")]
|
||||
#[test_case(CheckCode::F404, Path::new("F404.py"); "F404")]
|
||||
|
|
|
@ -40,13 +40,11 @@ pub fn remove_unused_imports(
|
|||
// Preserve the trailing comma (or not) from the last entry.
|
||||
let trailing_comma = aliases.last().and_then(|alias| alias.comma.clone());
|
||||
|
||||
// Identify unused imports from within the `import from`.
|
||||
// Identify unused imports from within the `import`.
|
||||
let mut removable = vec![];
|
||||
for (index, alias) in aliases.iter().enumerate() {
|
||||
if let NameOrAttribute::N(import_name) = &alias.name {
|
||||
if full_names.contains(&import_name.value) {
|
||||
removable.push(index);
|
||||
}
|
||||
if full_names.contains(&compose_module_path(&alias.name).as_str()) {
|
||||
removable.push(index);
|
||||
}
|
||||
}
|
||||
// TODO(charlie): This is quadratic.
|
||||
|
|
|
@ -51,13 +51,13 @@ expression: checks
|
|||
column: 24
|
||||
fix:
|
||||
patch:
|
||||
content: import logging.handlers
|
||||
content: ""
|
||||
location:
|
||||
row: 12
|
||||
column: 1
|
||||
end_location:
|
||||
row: 12
|
||||
column: 24
|
||||
row: 13
|
||||
column: 1
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
|
@ -135,23 +135,4 @@ expression: checks
|
|||
row: 53
|
||||
column: 22
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- foo.bar.baz
|
||||
location:
|
||||
row: 90
|
||||
column: 1
|
||||
end_location:
|
||||
row: 90
|
||||
column: 24
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 90
|
||||
column: 1
|
||||
end_location:
|
||||
row: 91
|
||||
column: 1
|
||||
applied: false
|
||||
|
||||
|
|
81
src/snapshots/ruff__linter__tests__F401_F401_5.py.snap
Normal file
81
src/snapshots/ruff__linter__tests__F401_F401_5.py.snap
Normal file
|
@ -0,0 +1,81 @@
|
|||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- a.b.c
|
||||
location:
|
||||
row: 2
|
||||
column: 1
|
||||
end_location:
|
||||
row: 2
|
||||
column: 18
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 2
|
||||
column: 1
|
||||
end_location:
|
||||
row: 3
|
||||
column: 1
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- d.e.f
|
||||
location:
|
||||
row: 3
|
||||
column: 1
|
||||
end_location:
|
||||
row: 3
|
||||
column: 23
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 3
|
||||
column: 1
|
||||
end_location:
|
||||
row: 4
|
||||
column: 1
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- h.i
|
||||
location:
|
||||
row: 4
|
||||
column: 1
|
||||
end_location:
|
||||
row: 4
|
||||
column: 11
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 4
|
||||
column: 1
|
||||
end_location:
|
||||
row: 5
|
||||
column: 1
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- j.k
|
||||
location:
|
||||
row: 5
|
||||
column: 1
|
||||
end_location:
|
||||
row: 5
|
||||
column: 16
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 5
|
||||
column: 1
|
||||
end_location:
|
||||
row: 6
|
||||
column: 1
|
||||
applied: false
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue