[ty] Add `from` imports to `imported_modules` *if* the module being imported is not relative to the current module

This commit is contained in:
Alex Waygood 2025-11-22 15:32:06 +00:00 committed by Aria Desires
parent 36c623300b
commit 6d4949bae1
2 changed files with 38 additions and 25 deletions

View File

@ -318,7 +318,7 @@ impl ModuleName {
db: &dyn Db,
importing_file: File,
) -> Result<Self, ModuleNameResolutionError> {
Self::from_identifier_parts(db, importing_file, None, 1)
relative_module_name(db, importing_file, None, NonZeroU32::new(1).unwrap())
}
}

View File

@ -1521,23 +1521,28 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
// that `x` can be freely overwritten, and that we don't assume that an import
// in one function is visible in another function.
let mut is_self_import = false;
if self.file.is_package(self.db)
&& let Ok(module_name) = ModuleName::from_identifier_parts(
let is_package = self.file.is_package(self.db);
let this_package = ModuleName::package_for_file(self.db, self.file);
if let Ok(module_name) = ModuleName::from_identifier_parts(
self.db,
self.file,
node.module.as_deref(),
node.level,
)
&& let Ok(thispackage) = ModuleName::package_for_file(self.db, self.file)
{
) {
// 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() {
if let Ok(thispackage) = this_package
&& let Some(relative_submodule) = module_name.relative_to(&thispackage)
&& let Some(direct_submodule) = relative_submodule.components().next()
{
if is_package
&& let Some(direct_submodule) =
relative_submodule.components().next()
&& !self.seen_submodule_imports.contains(direct_submodule)
&& self.current_scope().is_global()
{
self.seen_submodule_imports
.insert(direct_submodule.to_owned());
@ -1549,6 +1554,14 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
ImportFromSubmoduleDefinitionNodeRef { node },
);
}
} else {
self.imported_modules.extend(
module_name
.ancestors()
.zip(std::iter::repeat(ImportKind::ImportFrom)),
);
}
}
}
let mut found_star = false;