From 816bb88e3b2c88c9887706d95b4fc9fd363550d2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 28 Aug 2022 10:02:07 -0400 Subject: [PATCH] Use complete symbol for `import from` (#35) --- src/check_ast.rs | 31 +++++++++++++++++++++---------- src/linter.rs | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/check_ast.rs b/src/check_ast.rs index e7a24fd2e6..ed4a93534c 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -28,7 +28,7 @@ enum BindingKind { ClassDefinition, Definition, FutureImportation, - Importation, + Importation(String), StarImportation, SubmoduleImportation, } @@ -121,7 +121,13 @@ impl Visitor for Checker<'_> { }) } else { self.add_binding(Binding { - kind: BindingKind::Importation, + kind: BindingKind::Importation( + alias + .node + .asname + .clone() + .unwrap_or_else(|| alias.node.name.clone()), + ), name: alias .node .asname @@ -171,11 +177,14 @@ impl Visitor for Checker<'_> { } } else { self.add_binding(Binding { - kind: BindingKind::Importation, + kind: BindingKind::Importation(match module { + None => name.clone(), + Some(parent) => format!("{}.{}", parent, name), + }), name, used: false, location: stmt.location, - }); + }) } } } @@ -406,12 +415,14 @@ impl Checker<'_> { fn check_dead_scopes(&mut self) { // TODO(charlie): Handle `__all__`. for scope in &self.dead_scopes { - for (name, binding) in scope.values.iter().rev() { - if !binding.used && matches!(binding.kind, BindingKind::Importation) { - self.checks.push(Check { - kind: CheckKind::UnusedImport(name.clone()), - location: binding.location, - }); + for (_, binding) in scope.values.iter().rev() { + if !binding.used { + if let BindingKind::Importation(name) = &binding.kind { + self.checks.push(Check { + kind: CheckKind::UnusedImport(name.clone()), + location: binding.location, + }); + } } } } diff --git a/src/linter.rs b/src/linter.rs index 5b404d6149..57bc322331 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -113,7 +113,7 @@ mod tests { filename: "./resources/test/src/F401.py".to_string(), }, Message { - kind: CheckKind::UnusedImport("OrderedDict".to_string()), + kind: CheckKind::UnusedImport("collections.OrderedDict".to_string()), location: Location::new(3, 1), filename: "./resources/test/src/F401.py".to_string(), },