Skip to content

Commit

Permalink
[red-knot] Inline SubclassOfType::as_instance_type_of_metaclass() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jan 17, 2025
1 parent 1ba8e61 commit 4351d85
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 7 additions & 1 deletion crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down
9 changes: 0 additions & 9 deletions crates/red_knot_python_semantic/src/types/subclass_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4351d85

Please sign in to comment.