Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-segment import removal #480

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions resources/test/fixtures/F401_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,3 @@ def b(self) -> None:


CustomInt: TypeAlias = "np.int8 | np.int16"

from foo.bar import baz
5 changes: 5 additions & 0 deletions resources/test/fixtures/F401_5.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
8 changes: 3 additions & 5 deletions src/pyflakes/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 3 additions & 22 deletions src/snapshots/ruff__linter__tests__F401_F401_0.py.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 changes: 81 additions & 0 deletions src/snapshots/ruff__linter__tests__F401_F401_5.py.snap
Original file line number Diff line number Diff line change
@@ -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