[ty] Remove use of ClassBase::try_from_type from super() machinery (#19902)

This commit is contained in:
Alex Waygood 2025-08-14 22:14:31 +01:00 committed by GitHub
parent ce938fe205
commit 82350a398e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 16 deletions

View file

@ -9526,23 +9526,32 @@ impl<'db> BoundSuperType<'db> {
));
}
// TODO: having to get a class-literal just to pass it in here is silly.
// `BoundSuperType` should probably not be using `ClassBase::try_from_type` here;
// this also leads to false negatives in some cases. See discussion in
// <https://github.com/astral-sh/ruff/pull/19560#discussion_r2271570071>.
let pivot_class = ClassBase::try_from_type(
db,
pivot_class_type,
KnownClass::Object
.to_class_literal(db)
.into_class_literal()
.expect("`object` should always exist in typeshed"),
)
.ok_or({
BoundSuperError::InvalidPivotClassType {
pivot_class: pivot_class_type,
// We don't use `Classbase::try_from_type` here because:
// - There are objects that may validly be present in a class's bases list
// but are not valid as pivot classes, e.g. `typing.ChainMap`
// - There are objects that are not valid in a class's bases list
// but are valid as pivot classes, e.g. unsubscripted `typing.Generic`
let pivot_class = match pivot_class_type {
Type::ClassLiteral(class) => ClassBase::Class(ClassType::NonGeneric(class)),
Type::GenericAlias(class) => ClassBase::Class(ClassType::Generic(class)),
Type::SubclassOf(subclass_of) if subclass_of.subclass_of().is_dynamic() => {
ClassBase::Dynamic(
subclass_of
.subclass_of()
.into_dynamic()
.expect("Checked in branch arm"),
)
}
})?;
Type::SpecialForm(SpecialFormType::Protocol) => ClassBase::Protocol,
Type::SpecialForm(SpecialFormType::Generic) => ClassBase::Generic,
Type::SpecialForm(SpecialFormType::TypedDict) => ClassBase::TypedDict,
Type::Dynamic(dynamic) => ClassBase::Dynamic(dynamic),
_ => {
return Err(BoundSuperError::InvalidPivotClassType {
pivot_class: pivot_class_type,
});
}
};
let owner = SuperOwnerKind::try_from_type(db, owner_type)
.and_then(|owner| {