mirror of https://github.com/astral-sh/ruff
more
This commit is contained in:
parent
c8c915de00
commit
83a0e19e13
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue