mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 05:08:21 +00:00
Fix false-positive in submodule resolution (#6435)
Closes https://github.com/astral-sh/ruff/issues/6433.
This commit is contained in:
parent
1b9fed8397
commit
a2758513de
6 changed files with 67 additions and 12 deletions
|
@ -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>),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue