[red-knot]: Implement HasTy for Alias (#11971)

This commit is contained in:
Micha Reiser 2024-07-04 09:17:10 +02:00 committed by GitHub
parent 3ce8b9fcae
commit 262053f85c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)]
mod tests {
use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings};
@ -226,4 +238,26 @@ mod tests {
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(())
}
}