mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
[red-knot] type[T]
is disjoint from type[S]
if the metaclass of T
is disjoint from the metaclass of S
(#15547)
This commit is contained in:
parent
6771b8ebd2
commit
4328df7226
4 changed files with 59 additions and 21 deletions
|
@ -1276,21 +1276,6 @@ impl<'db> Type<'db> {
|
|||
ClassBase::Class(class_a) => !class_b.is_subclass_of(db, class_a),
|
||||
},
|
||||
|
||||
(Type::SubclassOf(_), Type::SubclassOf(_)) => false,
|
||||
|
||||
(Type::SubclassOf(subclass_of_ty), instance @ Type::Instance(_))
|
||||
| (instance @ Type::Instance(_), Type::SubclassOf(subclass_of_ty)) => {
|
||||
// `type[T]` is disjoint from `S`, where `S` is an instance type,
|
||||
// if `U` is disjoint from `S`,
|
||||
// where `U` represents all instances of `T`'s metaclass
|
||||
let metaclass_instance = subclass_of_ty
|
||||
.subclass_of()
|
||||
.into_class()
|
||||
.map(|class| class.metaclass(db).to_instance(db))
|
||||
.unwrap_or_else(|| KnownClass::Type.to_instance(db));
|
||||
instance.is_disjoint_from(db, metaclass_instance)
|
||||
}
|
||||
|
||||
(
|
||||
Type::SubclassOf(_),
|
||||
Type::BooleanLiteral(..)
|
||||
|
@ -1324,12 +1309,9 @@ impl<'db> Type<'db> {
|
|||
ty.bool(db).is_always_true()
|
||||
}
|
||||
|
||||
(Type::SubclassOf(_), other) | (other, Type::SubclassOf(_)) => {
|
||||
// TODO we could do better here: if both variants are `SubclassOf` and they have different "solid bases",
|
||||
// multiple inheritance between the two is impossible, so they are disjoint.
|
||||
//
|
||||
// Note that `type[<@final class>]` is eagerly simplified to `Literal[<@final class>]` by [`SubclassOfType::from`].
|
||||
other.is_disjoint_from(db, KnownClass::Type.to_instance(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))
|
||||
}
|
||||
|
||||
(Type::KnownInstance(known_instance), Type::Instance(InstanceType { class }))
|
||||
|
|
|
@ -68,6 +68,15 @@ 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue