From c00bd489f13c8cfbd2580e2bd99088b31f7dc1c4 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 26 Oct 2022 16:43:55 -0400 Subject: [PATCH] Fix multi-segment import removal (#480) --- resources/test/fixtures/F401_0.py | 2 - resources/test/fixtures/F401_5.py | 5 ++ src/linter.rs | 1 + src/pyflakes/fixes.rs | 8 +- .../ruff__linter__tests__F401_F401_0.py.snap | 25 +----- .../ruff__linter__tests__F401_F401_5.py.snap | 81 +++++++++++++++++++ 6 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 resources/test/fixtures/F401_5.py create mode 100644 src/snapshots/ruff__linter__tests__F401_F401_5.py.snap diff --git a/resources/test/fixtures/F401_0.py b/resources/test/fixtures/F401_0.py index d9e534fa6d..24afc4c6fe 100644 --- a/resources/test/fixtures/F401_0.py +++ b/resources/test/fixtures/F401_0.py @@ -86,5 +86,3 @@ else: CustomInt: TypeAlias = "np.int8 | np.int16" - -from foo.bar import baz diff --git a/resources/test/fixtures/F401_5.py b/resources/test/fixtures/F401_5.py new file mode 100644 index 0000000000..93bf8807d3 --- /dev/null +++ b/resources/test/fixtures/F401_5.py @@ -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 diff --git a/src/linter.rs b/src/linter.rs index 202c1155ae..2b4a48a804 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -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")] diff --git a/src/pyflakes/fixes.rs b/src/pyflakes/fixes.rs index b92043f1cc..2fdd75da5f 100644 --- a/src/pyflakes/fixes.rs +++ b/src/pyflakes/fixes.rs @@ -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. diff --git a/src/snapshots/ruff__linter__tests__F401_F401_0.py.snap b/src/snapshots/ruff__linter__tests__F401_F401_0.py.snap index 9f8c5c9c11..6de4b71043 100644 --- a/src/snapshots/ruff__linter__tests__F401_F401_0.py.snap +++ b/src/snapshots/ruff__linter__tests__F401_F401_0.py.snap @@ -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 diff --git a/src/snapshots/ruff__linter__tests__F401_F401_5.py.snap b/src/snapshots/ruff__linter__tests__F401_F401_5.py.snap new file mode 100644 index 0000000000..f4b486eed6 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__F401_F401_5.py.snap @@ -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 +