todo class bases

This commit is contained in:
Alex Waygood 2025-11-17 15:00:47 +00:00
parent 1fee6c3ede
commit ecda934c36
2 changed files with 20 additions and 17 deletions

View file

@ -38,7 +38,7 @@ use crate::types::{
ManualPEP695TypeAliasType, MaterializationKind, NormalizedVisitor, PropertyInstanceType,
StringLiteralType, TypeAliasType, TypeContext, TypeMapping, TypeRelation, TypedDictParams,
UnionBuilder, VarianceInferable, declaration_type, determine_upper_bound,
exceeds_max_specialization_depth, infer_definition_types,
exceeds_max_specialization_depth, infer_definition_types, todo_type,
};
use crate::{
Db, FxIndexMap, FxIndexSet, FxOrderSet, Program,
@ -1598,7 +1598,12 @@ impl<'db> ClassLiteral<'db> {
let class_definition =
semantic_index(db, self.file(db)).expect_single_definition(class_stmt);
if self.is_known(db, KnownClass::VersionInfo) {
if class_stmt.bases().iter().any(ast::Expr::is_starred_expr) {
return Box::new([todo_type!("Starred expressions in class bases")]);
}
match self.known(db) {
Some(KnownClass::VersionInfo) => {
let tuple_type = TupleType::new(db, &TupleSpec::version_info_spec(db))
.expect("sys.version_info tuple spec should always be a valid tuple");
@ -1606,12 +1611,15 @@ impl<'db> ClassLiteral<'db> {
definition_expression_type(db, class_definition, &class_stmt.bases()[0]),
Type::from(tuple_type.to_class_type(db)),
])
} else {
class_stmt
}
// Special-case `NotImplementedType`: typeshed says that it inherits from `Any`,
// but this causes more problems than it fixes.
Some(KnownClass::NotImplementedType) => Box::new([]),
_ => class_stmt
.bases()
.iter()
.map(|base_node| definition_expression_type(db, class_definition, base_node))
.collect()
.collect(),
}
}

View file

@ -7,7 +7,7 @@ use rustc_hash::FxBuildHasher;
use crate::Db;
use crate::types::class_base::ClassBase;
use crate::types::generics::Specialization;
use crate::types::{ClassLiteral, ClassType, KnownClass, KnownInstanceType, SpecialFormType, Type};
use crate::types::{ClassLiteral, ClassType, KnownInstanceType, SpecialFormType, Type};
/// The inferred method resolution order of a given class.
///
@ -52,11 +52,6 @@ impl<'db> Mro<'db> {
specialization: Option<Specialization<'db>>,
) -> Result<Self, MroError<'db>> {
let class = class_literal.apply_optional_specialization(db, specialization);
// Special-case `NotImplementedType`: typeshed says that it inherits from `Any`,
// but this causes more problems than it fixes.
if class_literal.is_known(db, KnownClass::NotImplementedType) {
return Ok(Self::from([ClassBase::Class(class), ClassBase::object(db)]));
}
Self::of_class_impl(db, class, class_literal.explicit_bases(db), specialization)
.map_err(|err| err.into_mro_error(db, class))
}