mirror of https://github.com/astral-sh/ruff
[ty] Add `from` imports to `imported_modules` *if* the module being imported is not relative to the current module
This commit is contained in:
parent
36c623300b
commit
6d4949bae1
|
|
@ -318,7 +318,7 @@ impl ModuleName {
|
||||||
db: &dyn Db,
|
db: &dyn Db,
|
||||||
importing_file: File,
|
importing_file: File,
|
||||||
) -> Result<Self, ModuleNameResolutionError> {
|
) -> Result<Self, ModuleNameResolutionError> {
|
||||||
Self::from_identifier_parts(db, importing_file, None, 1)
|
relative_module_name(db, importing_file, None, NonZeroU32::new(1).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1521,33 +1521,46 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
|
||||||
// that `x` can be freely overwritten, and that we don't assume that an import
|
// that `x` can be freely overwritten, and that we don't assume that an import
|
||||||
// in one function is visible in another function.
|
// in one function is visible in another function.
|
||||||
let mut is_self_import = false;
|
let mut is_self_import = false;
|
||||||
if self.file.is_package(self.db)
|
let is_package = self.file.is_package(self.db);
|
||||||
&& let Ok(module_name) = ModuleName::from_identifier_parts(
|
let this_package = ModuleName::package_for_file(self.db, self.file);
|
||||||
self.db,
|
|
||||||
self.file,
|
if let Ok(module_name) = ModuleName::from_identifier_parts(
|
||||||
node.module.as_deref(),
|
self.db,
|
||||||
node.level,
|
self.file,
|
||||||
)
|
node.module.as_deref(),
|
||||||
&& let Ok(thispackage) = ModuleName::package_for_file(self.db, self.file)
|
node.level,
|
||||||
{
|
) {
|
||||||
// Record whether this is equivalent to `from . import ...`
|
// Record whether this is equivalent to `from . import ...`
|
||||||
is_self_import = module_name == thispackage;
|
if is_package && let Ok(thispackage) = this_package.as_ref() {
|
||||||
|
is_self_import = &module_name == thispackage;
|
||||||
|
}
|
||||||
|
|
||||||
if node.module.is_some()
|
if self.current_scope().is_global() && node.module.is_some() {
|
||||||
&& let Some(relative_submodule) = module_name.relative_to(&thispackage)
|
if let Ok(thispackage) = this_package
|
||||||
&& let Some(direct_submodule) = relative_submodule.components().next()
|
&& let Some(relative_submodule) = module_name.relative_to(&thispackage)
|
||||||
&& !self.seen_submodule_imports.contains(direct_submodule)
|
{
|
||||||
&& self.current_scope().is_global()
|
if is_package
|
||||||
{
|
&& let Some(direct_submodule) =
|
||||||
self.seen_submodule_imports
|
relative_submodule.components().next()
|
||||||
.insert(direct_submodule.to_owned());
|
&& !self.seen_submodule_imports.contains(direct_submodule)
|
||||||
|
{
|
||||||
|
self.seen_submodule_imports
|
||||||
|
.insert(direct_submodule.to_owned());
|
||||||
|
|
||||||
let direct_submodule_name = Name::new(direct_submodule);
|
let direct_submodule_name = Name::new(direct_submodule);
|
||||||
let symbol = self.add_symbol(direct_submodule_name);
|
let symbol = self.add_symbol(direct_submodule_name);
|
||||||
self.add_definition(
|
self.add_definition(
|
||||||
symbol.into(),
|
symbol.into(),
|
||||||
ImportFromSubmoduleDefinitionNodeRef { node },
|
ImportFromSubmoduleDefinitionNodeRef { node },
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.imported_modules.extend(
|
||||||
|
module_name
|
||||||
|
.ancestors()
|
||||||
|
.zip(std::iter::repeat(ImportKind::ImportFrom)),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue