mirror of https://github.com/astral-sh/ruff
more
This commit is contained in:
parent
dd1ac81f15
commit
ac532e81e7
|
|
@ -647,8 +647,8 @@ reveal_type(mypackage.imported.X) # revealed: int
|
||||||
|
|
||||||
## `from` Import of Other Package's Submodule
|
## `from` Import of Other Package's Submodule
|
||||||
|
|
||||||
`from mypackage import submodule` from outside the package is not modeled as a side-effect on
|
`from mypackage import submodule` and `from mypackage.submodule import not_a_submodule` from outside
|
||||||
`mypackage`, even in the importing file (this could be changed!).
|
the package are both modeled as a side-effects on `mypackage`.
|
||||||
|
|
||||||
### In Stub
|
### In Stub
|
||||||
|
|
||||||
|
|
@ -663,18 +663,28 @@ reveal_type(mypackage.imported.X) # revealed: int
|
||||||
X: int = 42
|
X: int = 42
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`package2/__init__.pyi`:
|
||||||
|
|
||||||
|
```pyi
|
||||||
|
```
|
||||||
|
|
||||||
|
`package2/submodule.pyi`:
|
||||||
|
|
||||||
|
```pyi
|
||||||
|
not_a_submodule: int
|
||||||
|
```
|
||||||
|
|
||||||
`main.py`:
|
`main.py`:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import mypackage
|
import mypackage
|
||||||
|
import package2
|
||||||
from mypackage import imported
|
from mypackage import imported
|
||||||
|
from package2.submodule import not_a_submodule
|
||||||
|
|
||||||
reveal_type(imported.X) # revealed: int
|
reveal_type(imported.X) # revealed: int
|
||||||
|
reveal_type(mypackage.imported.X) # revealed: int
|
||||||
# TODO: this would be nice to support, but it's dangerous with available_submodule_attributes
|
reveal_type(package2.submodule.not_a_submodule) # revealed: int
|
||||||
# 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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### In Non-Stub
|
### In Non-Stub
|
||||||
|
|
@ -690,17 +700,28 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
|
||||||
X: int = 42
|
X: int = 42
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`package2/__init__.py`:
|
||||||
|
|
||||||
|
```py
|
||||||
|
```
|
||||||
|
|
||||||
|
`package2/submodule.py`:
|
||||||
|
|
||||||
|
```py
|
||||||
|
not_a_submodule: int
|
||||||
|
```
|
||||||
|
|
||||||
`main.py`:
|
`main.py`:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import mypackage
|
import mypackage
|
||||||
|
import package2
|
||||||
from mypackage import imported
|
from mypackage import imported
|
||||||
|
from package2.submodule import not_a_submodule
|
||||||
|
|
||||||
reveal_type(imported.X) # revealed: int
|
reveal_type(imported.X) # revealed: int
|
||||||
|
reveal_type(mypackage.imported.X) # revealed: int
|
||||||
# TODO: this would be nice to support, as it works at runtime
|
reveal_type(package2.submodule.not_a_submodule) # revealed: int
|
||||||
# error: [possibly-missing-attribute] "Submodule `imported` may not be available"
|
|
||||||
reveal_type(mypackage.imported.X) # revealed: Unknown
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## `from` Import of Sibling Module
|
## `from` Import of Sibling Module
|
||||||
|
|
|
||||||
|
|
@ -1556,6 +1556,16 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
|
||||||
.entry(name)
|
.entry(name)
|
||||||
.or_insert(ImportKind::ImportFrom);
|
.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