Respect multi-segment submodule imports when resolving qualified names (#9382)

Ensures that if the user has `import collections.abc`, then
`get_or_import_symbol` returns `collections.abc.Iterator` (or similar)
when requested.
This commit is contained in:
Charlie Marsh 2024-01-03 12:24:20 -04:00 committed by GitHub
parent 1ffc738c84
commit 7b6baff734
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 111 deletions

View file

@ -802,8 +802,10 @@ impl<'a> SemanticModel<'a> {
}
// Ex) Given `module="os"` and `object="name"`:
// `import os.path ` -> `os.name`
BindingKind::SubmoduleImport(SubmoduleImport { .. }) => {
if name == module {
// Ex) Given `module="os.path"` and `object="join"`:
// `import os.path ` -> `os.path.join`
BindingKind::SubmoduleImport(SubmoduleImport { call_path }) => {
if call_path.starts_with(&module_path) {
if let Some(source) = binding.source {
// Verify that `os` isn't bound in an inner scope.
if self
@ -812,7 +814,7 @@ impl<'a> SemanticModel<'a> {
.all(|scope| !scope.has(name))
{
return Some(ImportedName {
name: format!("{name}.{member}"),
name: format!("{module}.{member}"),
source,
range: self.nodes[source].range(),
context: binding.context,