Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading