mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
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:
parent
1ffc738c84
commit
7b6baff734
3 changed files with 68 additions and 111 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue