Support type[a.X] with qualified class names (#14825)

This adds support for `type[a.X]`, where the `type` special form is
applied to a qualified name that resolves to a class literal. This works
for both nested classes and classes imported from another module.

Closes #14545
This commit is contained in:
Douglas Creager 2024-12-06 17:14:51 -05:00 committed by GitHub
parent 3017b3b687
commit 8fdd88013d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 4 deletions

View file

@ -4655,18 +4655,18 @@ impl<'db> TypeInferenceBuilder<'db> {
/// Given the slice of a `type[]` annotation, return the type that the annotation represents
fn infer_subclass_of_type_expression(&mut self, slice: &ast::Expr) -> Type<'db> {
match slice {
ast::Expr::Name(_) => {
ast::Expr::Name(_) | ast::Expr::Attribute(_) => {
let name_ty = self.infer_expression(slice);
if let Some(ClassLiteralType { class }) = name_ty.into_class_literal() {
Type::subclass_of(class)
} else {
todo_type!()
todo_type!("unsupported type[X] special form")
}
}
// TODO: attributes, unions, subscripts, etc.
// TODO: unions, subscripts, etc.
_ => {
self.infer_type_expression(slice);
todo_type!()
todo_type!("unsupported type[X] special form")
}
}
}