This commit is contained in:
Alex Waygood 2025-11-23 17:15:50 +00:00 committed by Aria Desires
parent c8c915de00
commit 83a0e19e13
2 changed files with 42 additions and 11 deletions

View File

@ -647,8 +647,8 @@ reveal_type(mypackage.imported.X) # revealed: int
## `from` Import of Other Package's Submodule
`from mypackage import submodule` from outside the package is not modeled as a side-effect on
`mypackage`, even in the importing file (this could be changed!).
`from mypackage import submodule` and `from mypackage.submodule import not_a_submodule` from outside
the package are both modeled as a side-effects on `mypackage`.
### In Stub
@ -663,18 +663,28 @@ reveal_type(mypackage.imported.X) # revealed: int
X: int = 42
```
`package2/__init__.pyi`:
```pyi
```
`package2/submodule.pyi`:
```pyi
not_a_submodule: int
```
`main.py`:
```py
import mypackage
import package2
from mypackage import imported
from package2.submodule import not_a_submodule
reveal_type(imported.X) # revealed: int
# TODO: this would be nice to support, but it's dangerous with available_submodule_attributes
# for details, see: https://github.com/astral-sh/ty/issues/1488
# error: [possibly-missing-attribute] "Submodule `imported` may not be available"
reveal_type(mypackage.imported.X) # revealed: Unknown
reveal_type(mypackage.imported.X) # revealed: int
reveal_type(package2.submodule.not_a_submodule) # revealed: int
```
### In Non-Stub
@ -690,17 +700,28 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
X: int = 42
```
`package2/__init__.py`:
```py
```
`package2/submodule.py`:
```py
not_a_submodule: int
```
`main.py`:
```py
import mypackage
import package2
from mypackage import imported
from package2.submodule import not_a_submodule
reveal_type(imported.X) # revealed: int
# TODO: this would be nice to support, as it works at runtime
# error: [possibly-missing-attribute] "Submodule `imported` may not be available"
reveal_type(mypackage.imported.X) # revealed: Unknown
reveal_type(mypackage.imported.X) # revealed: int
reveal_type(package2.submodule.not_a_submodule) # revealed: int
```
## `from` Import of Sibling Module

View File

@ -1560,6 +1560,16 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
.entry(name)
.or_insert(ImportKind::ImportFrom);
}
for name in &node.names {
let Some(relative_name) = ModuleName::new(&name.name) else {
continue;
};
let mut full_name = module_name.clone();
full_name.extend(&relative_name);
self.imported_modules
.entry(full_name)
.or_insert(ImportKind::ImportFrom);
}
}
}
}