From 2be369f299e19f5daaafcafad517843db1559bb5 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Tue, 19 Aug 2025 19:44:18 -0700 Subject: [PATCH] is_subclass_of --- crates/ty_python_semantic/src/types/class.rs | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs index 50f236ff60..c211d14e8b 100644 --- a/crates/ty_python_semantic/src/types/class.rs +++ b/crates/ty_python_semantic/src/types/class.rs @@ -1405,6 +1405,19 @@ impl<'db> ClassSingletonType<'db> { Self::NewType(new_type) => new_type.dataclass_transformer_params(db), } } + + pub(super) fn is_subclass_of( + self, + db: &'db dyn Db, + specialization: Option>, + other: ClassType<'db>, + ) -> bool { + match self { + Self::Literal(literal) => literal.is_subclass_of(db, specialization, other), + // A NewType can't be specialized. + Self::NewType(new_type) => new_type.is_subclass_of(db, other), + } + } } impl<'db> From> for Type<'db> { @@ -3249,7 +3262,7 @@ pub struct NewTypeClass<'db> { #[returns(ref)] name: Name, - parent: Box>, + parent: ClassType<'db>, } impl<'db> NewTypeClass<'db> { @@ -3332,6 +3345,15 @@ impl<'db> NewTypeClass<'db> { ) -> Option { self.parent(db).dataclass_transformer_params(db) } + + pub(super) fn is_subclass_of(self, db: &'db dyn Db, other: ClassType<'db>) -> bool { + let parent = self.parent(db); + if parent == other { + true + } else { + parent.is_subclass_of(db, other) + } + } } impl<'db> get_size2::GetSize for NewTypeClass<'_> {}