mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
[red-knot]: Implement HasTy
for Alias
(#11971)
This commit is contained in:
parent
3ce8b9fcae
commit
262053f85c
1 changed files with 34 additions and 0 deletions
|
@ -163,6 +163,18 @@ impl HasTy for StmtClassDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HasTy for ast::Alias {
|
||||||
|
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
|
||||||
|
let index = semantic_index(model.db, model.file);
|
||||||
|
let definition = index.definition(self);
|
||||||
|
|
||||||
|
let scope = definition.scope(model.db).to_scope_id(model.db, model.file);
|
||||||
|
let types = infer_types(model.db, scope);
|
||||||
|
|
||||||
|
types.definition_ty(definition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings};
|
use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings};
|
||||||
|
@ -226,4 +238,26 @@ mod tests {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_ty() -> anyhow::Result<()> {
|
||||||
|
let db = setup_db();
|
||||||
|
|
||||||
|
db.memory_file_system().write_files([
|
||||||
|
("/src/foo.py", "class Test: pass"),
|
||||||
|
("/src/bar.py", "from foo import Test"),
|
||||||
|
])?;
|
||||||
|
let bar = system_path_to_file(&db, "/src/bar.py").unwrap();
|
||||||
|
|
||||||
|
let ast = parsed_module(&db, bar);
|
||||||
|
|
||||||
|
let import = ast.suite()[0].as_import_from_stmt().unwrap();
|
||||||
|
let alias = &import.names[0];
|
||||||
|
let model = SemanticModel::new(&db, bar);
|
||||||
|
let ty = alias.ty(&model);
|
||||||
|
|
||||||
|
assert!(matches!(ty, Type::Class(_)));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue