mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 18:02:23 +00:00
Detect basic wildcard imports in ruff analyze graph (#13486)
## Summary I guess we can just ignore the `*` entirely for now? This will add the `__init__.py` for anything that's importing a package.
This commit is contained in:
parent
96e7f3f96f
commit
ff4b6d11fa
2 changed files with 66 additions and 2 deletions
|
@ -305,3 +305,65 @@ fn exclude() -> Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn wildcard() -> Result<()> {
|
||||||
|
let tempdir = TempDir::new()?;
|
||||||
|
let root = ChildPath::new(tempdir.path());
|
||||||
|
|
||||||
|
root.child("ruff").child("__init__.py").write_str("")?;
|
||||||
|
root.child("ruff")
|
||||||
|
.child("a.py")
|
||||||
|
.write_str(indoc::indoc! {r#"
|
||||||
|
from ruff.b import *
|
||||||
|
"#})?;
|
||||||
|
root.child("ruff")
|
||||||
|
.child("b.py")
|
||||||
|
.write_str(indoc::indoc! {r#"
|
||||||
|
from ruff import c
|
||||||
|
"#})?;
|
||||||
|
root.child("ruff")
|
||||||
|
.child("c.py")
|
||||||
|
.write_str(indoc::indoc! {r#"
|
||||||
|
from ruff.utils import *
|
||||||
|
"#})?;
|
||||||
|
|
||||||
|
root.child("ruff")
|
||||||
|
.child("utils")
|
||||||
|
.child("__init__.py")
|
||||||
|
.write_str("from .helpers import *")?;
|
||||||
|
root.child("ruff")
|
||||||
|
.child("utils")
|
||||||
|
.child("helpers.py")
|
||||||
|
.write_str("")?;
|
||||||
|
|
||||||
|
insta::with_settings!({
|
||||||
|
filters => INSTA_FILTERS.to_vec(),
|
||||||
|
}, {
|
||||||
|
assert_cmd_snapshot!(command().current_dir(&root), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
{
|
||||||
|
"ruff/__init__.py": [],
|
||||||
|
"ruff/a.py": [
|
||||||
|
"ruff/b.py"
|
||||||
|
],
|
||||||
|
"ruff/b.py": [
|
||||||
|
"ruff/c.py"
|
||||||
|
],
|
||||||
|
"ruff/c.py": [
|
||||||
|
"ruff/utils/__init__.py"
|
||||||
|
],
|
||||||
|
"ruff/utils/__init__.py": [
|
||||||
|
"ruff/utils/helpers.py"
|
||||||
|
],
|
||||||
|
"ruff/utils/helpers.py": []
|
||||||
|
}
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -90,8 +90,10 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
|
||||||
components.extend(module.split('.'));
|
components.extend(module.split('.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the alias name.
|
// Add the alias name, unless it's a wildcard import.
|
||||||
components.push(alias.name.as_str());
|
if alias.name.as_str() != "*" {
|
||||||
|
components.push(alias.name.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(module_name) = ModuleName::from_components(components) {
|
if let Some(module_name) = ModuleName::from_components(components) {
|
||||||
self.imports.push(CollectedImport::ImportFrom(module_name));
|
self.imports.push(CollectedImport::ImportFrom(module_name));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue