Fix false-positive in submodule resolution (#6435)

Closes https://github.com/astral-sh/ruff/issues/6433.
This commit is contained in:
Charlie Marsh 2023-08-08 22:36:39 -04:00 committed by GitHub
parent 1b9fed8397
commit a2758513de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 12 deletions

View file

@ -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>),

View file

@ -590,14 +590,26 @@ 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;
}
// Ensure that the submodule import and the aliased import are from the same module.
if import.module_name() != submodule.module_name() {
return None;
}