[red-knot] Inline SubclassOfType::as_instance_type_of_metaclass() (#15556)

This commit is contained in:
Alex Waygood 2025-01-17 19:01:36 +00:00 committed by GitHub
parent 1ba8e61875
commit 4351d85d24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 10 deletions

View file

@ -1311,7 +1311,13 @@ impl<'db> Type<'db> {
(Type::SubclassOf(subclass_of_ty), other)
| (other, Type::SubclassOf(subclass_of_ty)) => {
other.is_disjoint_from(db, subclass_of_ty.as_instance_type_of_metaclass(db))
let metaclass_instance_ty = match subclass_of_ty.subclass_of() {
// for `type[Any]`/`type[Unknown]`/`type[Todo]`, we know the type cannot be any larger than `type`,
// so although the type is dynamic we can still determine disjointness in some situations
ClassBase::Dynamic(_) => KnownClass::Type.to_instance(db),
ClassBase::Class(class) => class.metaclass(db).to_instance(db),
};
other.is_disjoint_from(db, metaclass_instance_ty)
}
(Type::KnownInstance(known_instance), Type::Instance(InstanceType { class }))

View file

@ -68,15 +68,6 @@ impl<'db> SubclassOfType<'db> {
Type::from(self.subclass_of).member(db, name)
}
/// A class `T` is an instance of its metaclass `U`,
/// so the type `type[T]` is a subtype of the instance type `U`.
pub(crate) fn as_instance_type_of_metaclass(&self, db: &'db dyn Db) -> Type<'db> {
match self.subclass_of {
ClassBase::Dynamic(_) => KnownClass::Type.to_instance(db),
ClassBase::Class(class) => class.metaclass(db).to_instance(db),
}
}
/// Return `true` if `self` is a subtype of `other`.
///
/// This can only return `true` if `self.subclass_of` is a [`ClassBase::Class`] variant;