mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 21:28:21 +00:00
[ty] Fix relative imports in stub packages (#18132)
This commit is contained in:
parent
e677cabd69
commit
1ba56b4bc6
2 changed files with 13 additions and 4 deletions
|
@ -311,6 +311,5 @@ class YamlLoader: ...
|
|||
```py
|
||||
import yaml
|
||||
|
||||
# TODO: This should not be an error
|
||||
yaml.YamlLoader # error: [unresolved-attribute] "Type `<module 'yaml'>` has no attribute `YamlLoader`"
|
||||
reveal_type(yaml.YamlLoader) # revealed: <class 'YamlLoader'>
|
||||
```
|
||||
|
|
|
@ -4,7 +4,6 @@ use std::fmt;
|
|||
use std::sync::Arc;
|
||||
|
||||
use camino::{Utf8Path, Utf8PathBuf};
|
||||
|
||||
use ruff_db::files::{File, FileError, system_path_to_file, vendored_path_to_file};
|
||||
use ruff_db::system::{System, SystemPath, SystemPathBuf};
|
||||
use ruff_db::vendored::{VendoredPath, VendoredPathBuf};
|
||||
|
@ -189,7 +188,18 @@ impl ModulePath {
|
|||
stdlib_path_to_module_name(relative_path)
|
||||
} else {
|
||||
let parent = relative_path.parent()?;
|
||||
let parent_components = parent.components().map(|component| component.as_str());
|
||||
let parent_components = parent.components().enumerate().map(|(index, component)| {
|
||||
let component = component.as_str();
|
||||
|
||||
// For stub packages, strip the `-stubs` suffix from the first component
|
||||
// because it isn't a valid module name part AND the module name is the name without the `-stubs`.
|
||||
if index == 0 {
|
||||
component.strip_suffix("-stubs").unwrap_or(component)
|
||||
} else {
|
||||
component
|
||||
}
|
||||
});
|
||||
|
||||
let skip_final_part =
|
||||
relative_path.ends_with("__init__.py") || relative_path.ends_with("__init__.pyi");
|
||||
if skip_final_part {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue