Skip to content

Commit

Permalink
remove the qualified-name field from UnusedImport in favor of the bin…
Browse files Browse the repository at this point in the history
…ding-name field (named as .name); update fixture test results
  • Loading branch information
plredmond committed May 14, 2024
1 parent 01e974f commit 0696df5
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 87 deletions.
23 changes: 6 additions & 17 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ enum UnusedImportContext {
/// - [Typing documentation: interface conventions](https://typing.readthedocs.io/en/latest/source/libraries.html#library-interface-public-and-private-symbols)
#[violation]
pub struct UnusedImport {
/// Qualified name of the import
name: String,
/// Name of the import binding
binding: String,
name: String,
context: Option<UnusedImportContext>,
multiple: bool,
}
Expand All @@ -115,24 +113,17 @@ impl Violation for UnusedImport {
}

fn fix_title(&self) -> Option<String> {
let UnusedImport {
name,
binding,
multiple,
..
} = self;
let UnusedImport { name, multiple, .. } = self;
match self.context {
Some(UnusedImportContext::Init {
first_party: true,
dunder_all_count: 1,
}) => Some(format!("Add unused import `{binding}` to __all__")),
}) => Some(format!("Add unused import `{name}` to __all__")),

Some(UnusedImportContext::Init {
first_party: true,
dunder_all_count: 0,
}) => Some(format!(
"Use an explicit re-export: `{binding} as {binding}`"
)),
}) => Some(format!("Use an explicit re-export: `{name} as {name}`")),

_ => Some(if *multiple {
"Remove unused import".to_string()
Expand Down Expand Up @@ -319,8 +310,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
) {
let mut diagnostic = Diagnostic::new(
UnusedImport {
name: binding.import.qualified_name().to_string(),
binding: binding.name.to_string(),
name: binding.name.to_string(),
context,
multiple,
},
Expand All @@ -343,8 +333,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
for binding in ignored.into_values().flatten() {
let mut diagnostic = Diagnostic::new(
UnusedImport {
name: binding.import.qualified_name().to_string(),
binding: binding.name.to_string(),
name: binding.name.to_string(),
context: None,
multiple: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ F401_0.py:2:8: F401 [*] `functools` imported but unused
4 4 | from collections import (
5 5 | Counter,

F401_0.py:6:5: F401 [*] `collections.OrderedDict` imported but unused
F401_0.py:6:5: F401 [*] `OrderedDict` imported but unused
|
4 | from collections import (
5 | Counter,
Expand All @@ -28,7 +28,7 @@ F401_0.py:6:5: F401 [*] `collections.OrderedDict` imported but unused
7 | namedtuple,
8 | )
|
= help: Remove unused import: `collections.OrderedDict`
= help: Remove unused import: `OrderedDict`

Safe fix
3 3 | from datetime import datetime
Expand Down Expand Up @@ -268,19 +268,17 @@ F401_0.py:114:16: F401 [*] `b2` imported but unused
116 115 |
117 116 | # Regression test for: https://github.com/astral-sh/ruff/issues/7244

F401_0.py:122:1: F401 [*] `datameta_client_lib.model_helpers.noqa` imported but unused
F401_0.py:122:1: F401 [*] `noqa` imported but unused
|
121 | from datameta_client_lib.model_helpers import (
122 | noqa )
| ^^^^ F401
|
= help: Remove unused import: `datameta_client_lib.model_helpers.noqa`
= help: Remove unused import: `noqa`

Safe fix
118 118 | from datameta_client_lib.model_utils import ( # noqa: F401
119 119 | noqa )
120 120 |
121 |-from datameta_client_lib.model_helpers import (
122 |-noqa )


Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_11.py:4:27: F401 [*] `pathlib.PurePath` imported but unused
F401_11.py:4:27: F401 [*] `PurePath` imported but unused
|
3 | from typing import List
4 | from pathlib import Path, PurePath
| ^^^^^^^^ F401
|
= help: Remove unused import: `pathlib.PurePath`
= help: Remove unused import: `PurePath`

Safe fix
1 1 | """Test: parsing of nested string annotations."""
Expand All @@ -18,5 +18,3 @@ F401_11.py:4:27: F401 [*] `pathlib.PurePath` imported but unused
5 5 |
6 6 |
7 7 | x: """List['Path']""" = []


Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_15.py:5:25: F401 [*] `pathlib.Path` imported but unused
F401_15.py:5:25: F401 [*] `Path` imported but unused
|
4 | if TYPE_CHECKING:
5 | from pathlib import Path
| ^^^^ F401
|
= help: Remove unused import: `pathlib.Path`
= help: Remove unused import: `Path`

Safe fix
2 2 | from django.db.models import ForeignKey
Expand All @@ -18,5 +18,3 @@ F401_15.py:5:25: F401 [*] `pathlib.Path` imported but unused
6 6 |
7 7 |
8 8 | class Foo:


Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_17.py:12:27: F401 [*] `threading.Thread` imported but unused
F401_17.py:12:27: F401 [*] `Thread` imported but unused
|
11 | def fn(thread: Thread):
12 | from threading import Thread
| ^^^^^^ F401
13 |
14 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the
|
= help: Remove unused import: `threading.Thread`
= help: Remove unused import: `Thread`

Safe fix
9 9 |
Expand All @@ -20,15 +20,15 @@ F401_17.py:12:27: F401 [*] `threading.Thread` imported but unused
14 13 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the
15 14 | # top level.

F401_17.py:20:27: F401 [*] `threading.Thread` imported but unused
F401_17.py:20:27: F401 [*] `Thread` imported but unused
|
19 | def fn(thread: Thread):
20 | from threading import Thread
| ^^^^^^ F401
21 |
22 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the
|
= help: Remove unused import: `threading.Thread`
= help: Remove unused import: `Thread`

Safe fix
17 17 |
Expand All @@ -38,5 +38,3 @@ F401_17.py:20:27: F401 [*] `threading.Thread` imported but unused
21 20 |
22 21 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the
23 22 | # top level.


Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_23.py:4:16: F401 [*] `re.RegexFlag` imported but unused
F401_23.py:4:16: F401 [*] `RegexFlag` imported but unused
|
3 | from pathlib import Path
4 | from re import RegexFlag
| ^^^^^^^^^ F401
5 | from typing import Annotated
|
= help: Remove unused import: `re.RegexFlag`
= help: Remove unused import: `RegexFlag`

Safe fix
1 1 | """Test: ensure that we treat strings in `typing.Annotation` as type definitions."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_5.py:2:17: F401 [*] `a.b.c` imported but unused
F401_5.py:2:17: F401 [*] `c` imported but unused
|
1 | """Test: removal of multi-segment and aliases imports."""
2 | from a.b import c
| ^ F401
3 | from d.e import f as g
4 | import h.i
|
= help: Remove unused import: `a.b.c`
= help: Remove unused import: `c`

Safe fix
1 1 | """Test: removal of multi-segment and aliases imports."""
Expand All @@ -18,7 +18,7 @@ F401_5.py:2:17: F401 [*] `a.b.c` imported but unused
4 3 | import h.i
5 4 | import j.k as l

F401_5.py:3:22: F401 [*] `d.e.f` imported but unused
F401_5.py:3:22: F401 [*] `g` imported but unused
|
1 | """Test: removal of multi-segment and aliases imports."""
2 | from a.b import c
Expand All @@ -27,7 +27,7 @@ F401_5.py:3:22: F401 [*] `d.e.f` imported but unused
4 | import h.i
5 | import j.k as l
|
= help: Remove unused import: `d.e.f`
= help: Remove unused import: `g`

Safe fix
1 1 | """Test: removal of multi-segment and aliases imports."""
Expand All @@ -53,19 +53,17 @@ F401_5.py:4:8: F401 [*] `h.i` imported but unused
4 |-import h.i
5 4 | import j.k as l

F401_5.py:5:15: F401 [*] `j.k` imported but unused
F401_5.py:5:15: F401 [*] `l` imported but unused
|
3 | from d.e import f as g
4 | import h.i
5 | import j.k as l
| ^ F401
|
= help: Remove unused import: `j.k`
= help: Remove unused import: `l`

Safe fix
2 2 | from a.b import c
3 3 | from d.e import f as g
4 4 | import h.i
5 |-import j.k as l


Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_6.py:7:25: F401 [*] `.background.BackgroundTasks` imported but unused
F401_6.py:7:25: F401 [*] `BackgroundTasks` imported but unused
|
6 | # F401 `background.BackgroundTasks` imported but unused
7 | from .background import BackgroundTasks
| ^^^^^^^^^^^^^^^ F401
8 |
9 | # F401 `datastructures.UploadFile` imported but unused
|
= help: Remove unused import: `.background.BackgroundTasks`
= help: Remove unused import: `BackgroundTasks`

Safe fix
4 4 | from .applications import FastAPI as FastAPI
Expand All @@ -20,15 +20,15 @@ F401_6.py:7:25: F401 [*] `.background.BackgroundTasks` imported but unused
9 8 | # F401 `datastructures.UploadFile` imported but unused
10 9 | from .datastructures import UploadFile as FileUpload

F401_6.py:10:43: F401 [*] `.datastructures.UploadFile` imported but unused
F401_6.py:10:43: F401 [*] `FileUpload` imported but unused
|
9 | # F401 `datastructures.UploadFile` imported but unused
10 | from .datastructures import UploadFile as FileUpload
| ^^^^^^^^^^ F401
11 |
12 | # OK
|
= help: Remove unused import: `.datastructures.UploadFile`
= help: Remove unused import: `FileUpload`

Safe fix
7 7 | from .background import BackgroundTasks
Expand Down Expand Up @@ -58,18 +58,16 @@ F401_6.py:16:8: F401 [*] `background` imported but unused
18 17 | # F401 `datastructures` imported but unused
19 18 | import datastructures as structures

F401_6.py:19:26: F401 [*] `datastructures` imported but unused
F401_6.py:19:26: F401 [*] `structures` imported but unused
|
18 | # F401 `datastructures` imported but unused
19 | import datastructures as structures
| ^^^^^^^^^^ F401
|
= help: Remove unused import: `datastructures`
= help: Remove unused import: `structures`

Safe fix
16 16 | import background
17 17 |
18 18 | # F401 `datastructures` imported but unused
19 |-import datastructures as structures


Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_7.py:30:5: F401 [*] `typing.Union` imported but unused
F401_7.py:30:5: F401 [*] `Union` imported but unused
|
28 | from typing import (
29 | Mapping, # noqa: F401
30 | Union,
| ^^^^^ F401
31 | )
|
= help: Remove unused import: `typing.Union`
= help: Remove unused import: `Union`

Safe fix
27 27 | # This should ignore the first error.
Expand All @@ -22,7 +22,7 @@ F401_7.py:30:5: F401 [*] `typing.Union` imported but unused
33 32 | # This should ignore both errors.
34 33 | from typing import ( # noqa

F401_7.py:66:20: F401 [*] `typing.Awaitable` imported but unused
F401_7.py:66:20: F401 [*] `Awaitable` imported but unused
|
65 | # This should mark F501 as unused.
66 | from typing import Awaitable, AwaitableGenerator # noqa: F501
Expand All @@ -36,7 +36,7 @@ F401_7.py:66:20: F401 [*] `typing.Awaitable` imported but unused
65 65 | # This should mark F501 as unused.
66 |-from typing import Awaitable, AwaitableGenerator # noqa: F501

F401_7.py:66:31: F401 [*] `typing.AwaitableGenerator` imported but unused
F401_7.py:66:31: F401 [*] `AwaitableGenerator` imported but unused
|
65 | # This should mark F501 as unused.
66 | from typing import Awaitable, AwaitableGenerator # noqa: F501
Expand All @@ -49,5 +49,3 @@ F401_7.py:66:31: F401 [*] `typing.AwaitableGenerator` imported but unused
64 64 |
65 65 | # This should mark F501 as unused.
66 |-from typing import Awaitable, AwaitableGenerator # noqa: F501


Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_9.py:4:22: F401 [*] `foo.baz` imported but unused
F401_9.py:4:22: F401 [*] `baz` imported but unused
|
3 | __all__ = ("bar",)
4 | from foo import bar, baz
| ^^^ F401
|
= help: Remove unused import: `foo.baz`
= help: Remove unused import: `baz`

Safe fix
1 1 | """Test: late-binding of `__all__`."""
2 2 |
3 3 | __all__ = ("bar",)
4 |-from foo import bar, baz
4 |+from foo import bar


Loading

0 comments on commit 0696df5

Please sign in to comment.