[red-knot] Minor simplifications to types.rs (#14962)

This commit is contained in:
Alex Waygood 2024-12-13 20:31:51 +00:00 committed by GitHub
parent 90a5439791
commit 224c8438bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 33 deletions

View file

@ -769,34 +769,10 @@ impl<'db> Type<'db> {
Type::subclass_of(class).is_subtype_of(db, target) Type::subclass_of(class).is_subtype_of(db, target)
} }
// As the `unreachable!()` message says, non-fully-static `SubclassOf` types such as // This branch asks: given two types `type[T]` and `type[S]`, is `type[T]` a subtype of `type[S]`?
// `type[Any]`,` type[Unknown]` and `type[Todo]` should all be handled right at the top of this function. (Type::SubclassOf(self_subclass_ty), Type::SubclassOf(target_subclass_ty)) => {
( self_subclass_ty.is_subtype_of(db, target_subclass_ty)
Type::SubclassOf(SubclassOfType { }
base: ClassBase::Any | ClassBase::Unknown | ClassBase::Todo(_),
}),
_,
)
| (
_,
Type::SubclassOf(SubclassOfType {
base: ClassBase::Any | ClassBase::Unknown | ClassBase::Todo(_),
}),
) => unreachable!(
"Non-fully-static types should be handled at the top of this function!"
),
// For example, `type[bool]` describes all possible runtime subclasses of the class `bool`,
// and `type[int]` describes all possible runtime subclasses of the class `int`.
// The first set is a subset of the second set, because `bool` is itself a subclass of `int`.
(
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(self_class),
}),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(target_class),
}),
) => self_class.is_subclass_of(db, target_class),
// `type[str]` (== `SubclassOf("str")` in red-knot) describes all possible runtime subclasses // `type[str]` (== `SubclassOf("str")` in red-knot) describes all possible runtime subclasses
// of the class object `str`. It is a subtype of `type` (== `Instance("type")`) because `str` // of the class object `str`. It is a subtype of `type` (== `Instance("type")`) because `str`
@ -1046,9 +1022,8 @@ impl<'db> Type<'db> {
| Type::SliceLiteral(..) | Type::SliceLiteral(..)
| Type::StringLiteral(..) | Type::StringLiteral(..)
| Type::LiteralString, | Type::LiteralString,
) => true, )
| (
(
Type::ClassLiteral(..) Type::ClassLiteral(..)
| Type::ModuleLiteral(..) | Type::ModuleLiteral(..)
| Type::BooleanLiteral(..) | Type::BooleanLiteral(..)
@ -3128,6 +3103,22 @@ impl<'db> SubclassOfType<'db> {
fn member(self, db: &'db dyn Db, name: &str) -> Symbol<'db> { fn member(self, db: &'db dyn Db, name: &str) -> Symbol<'db> {
Type::from(self.base).member(db, name) Type::from(self.base).member(db, name)
} }
fn is_subtype_of(self, db: &'db dyn Db, other: SubclassOfType<'db>) -> bool {
match (self.base, other.base) {
// Non-fully-static types do not participate in subtyping
(ClassBase::Any | ClassBase::Unknown | ClassBase::Todo(_), _)
| (_, ClassBase::Any | ClassBase::Unknown | ClassBase::Todo(_)) => false,
// For example, `type[bool]` describes all possible runtime subclasses of the class `bool`,
// and `type[int]` describes all possible runtime subclasses of the class `int`.
// The first set is a subset of the second set, because `bool` is itself a subclass of `int`.
(ClassBase::Class(self_class), ClassBase::Class(other_class)) => {
// N.B. The subclass relation is fully static
self_class.is_subclass_of(db, other_class)
}
}
}
} }
/// A type representing the set of runtime objects which are instances of a certain class. /// A type representing the set of runtime objects which are instances of a certain class.
@ -3138,6 +3129,7 @@ pub struct InstanceType<'db> {
impl<'db> InstanceType<'db> { impl<'db> InstanceType<'db> {
fn is_subtype_of(self, db: &'db dyn Db, other: InstanceType<'db>) -> bool { fn is_subtype_of(self, db: &'db dyn Db, other: InstanceType<'db>) -> bool {
// N.B. The subclass relation is fully static
self.class.is_subclass_of(db, other.class) self.class.is_subclass_of(db, other.class)
} }
} }

View file

@ -125,7 +125,7 @@ impl<'db> ClassBase<'db> {
} }
} }
pub(super) fn into_class_literal_type(self) -> Option<Class<'db>> { pub(super) fn into_class(self) -> Option<Class<'db>> {
match self { match self {
Self::Class(class) => Some(class), Self::Class(class) => Some(class),
_ => None, _ => None,

View file

@ -117,7 +117,7 @@ impl<'db> Mro<'db> {
for (index, base) in valid_bases for (index, base) in valid_bases
.iter() .iter()
.enumerate() .enumerate()
.filter_map(|(index, base)| Some((index, base.into_class_literal_type()?))) .filter_map(|(index, base)| Some((index, base.into_class()?)))
{ {
if !seen_bases.insert(base) { if !seen_bases.insert(base) {
duplicate_bases.push((index, base)); duplicate_bases.push((index, base));