Skip to content

Commit

Permalink
Fix false-positive in submodule resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 9, 2023
1 parent 1b9fed8 commit 2d0be49
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F401_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ def b(self) -> None:
case 0,:
import x
import y


# Test: access a sub-importation via an alias.
import foo.bar as bop
import foo.bar.baz

print(bop.baz.read_csv("test.csv"))
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F823.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ def main():

def requests_mock(requests_mock: rm.Mocker):
print(rm.ANY)


import sklearn.base
import mlflow.sklearn


def f():
import sklearn

mlflow
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ F401_0.py:93:16: F401 [*] `x` imported but unused
92 92 | case 0,:
93 |- import x
94 93 | import y
95 94 |
96 95 |

F401_0.py:94:16: F401 [*] `y` imported but unused
|
Expand All @@ -166,5 +168,27 @@ F401_0.py:94:16: F401 [*] `y` imported but unused
92 92 | case 0,:
93 93 | import x
94 |- import y
95 94 |
96 95 |
97 96 | # Test: access a sub-importation via an alias.

F401_0.py:99:8: F401 [*] `foo.bar.baz` imported but unused
|
97 | # Test: access a sub-importation via an alias.
98 | import foo.bar as bop
99 | import foo.bar.baz
| ^^^^^^^^^^^ F401
100 |
101 | print(bop.baz.read_csv("test.csv"))
|
= help: Remove unused import: `foo.bar.baz`

Fix
96 96 |
97 97 | # Test: access a sub-importation via an alias.
98 98 | import foo.bar as bop
99 |-import foo.bar.baz
100 99 |
101 100 | print(bop.baz.read_csv("test.csv"))


22 changes: 12 additions & 10 deletions crates/ruff/src/rules/pyupgrade/rules/use_pep695_type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,18 @@ impl<'a> Visitor<'a> for TypeVarReferenceVisitor<'a> {
fn visit_expr(&mut self, expr: &'a Expr) {
match expr {
Expr::Name(name) if name.ctx.is_load() => {
let Some(Stmt::Assign(StmtAssign { value, .. })) =
self.semantic.lookup_symbol(name.id.as_str())
.and_then(|binding_id| {
self.semantic
.binding(binding_id)
.source
.map(|node_id| self.semantic.statement(node_id))
}) else {
return;
};
let Some(Stmt::Assign(StmtAssign { value, .. })) = self
.semantic
.lookup_symbol(name.id.as_str())
.and_then(|binding_id| {
self.semantic
.binding(binding_id)
.source
.map(|node_id| self.semantic.statement(node_id))
})
else {
return;
};

match value.as_ref() {
Expr::Subscript(ExprSubscript {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl<'a> Imported<'a> for FromImport<'a> {
}

/// A wrapper around an import [`BindingKind`] that can be any of the three types of imports.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, is_macro::Is)]
pub enum AnyImport<'a> {
Import(&'a Import<'a>),
SubmoduleImport(&'a SubmoduleImport<'a>),
Expand Down
12 changes: 11 additions & 1 deletion crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,24 @@ impl<'a> SemanticModel<'a> {
// print(pa.csv.read_csv("test.csv"))
// ```
let import = self.bindings[binding_id].as_any_import()?;
if !import.is_import() {
return None;
}

// Grab, e.g., `pyarrow` from `import pyarrow as pa`.
let call_path = import.call_path();
let segment = call_path.last()?;
if *segment == symbol {
return None;
}

// Locate the submodule import (e.g., `pyarrow.csv`) that `pa` aliases.
let binding_id = self.scopes[scope_id].get(segment)?;
if !self.bindings[binding_id].kind.is_submodule_import() {
let submodule = &self.bindings[binding_id].as_any_import()?;
if !submodule.is_submodule_import() {
return None;
}
if import.module_name() != submodule.module_name() {
return None;
}

Expand Down

0 comments on commit 2d0be49

Please sign in to comment.