[ty] Remove Salsa interning for TypedDictType (#19879)

This commit is contained in:
Alex Waygood 2025-08-12 14:35:26 +01:00 committed by GitHub
parent 94cfdf4b40
commit 3458f365da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 31 deletions

View file

@ -1015,6 +1015,12 @@ impl<'db> Type<'db> {
Self::BytesLiteral(BytesLiteralType::new(db, bytes)) Self::BytesLiteral(BytesLiteralType::new(db, bytes))
} }
pub(crate) fn typed_dict(defining_class: impl Into<ClassType<'db>>) -> Self {
Self::TypedDict(TypedDictType {
defining_class: defining_class.into(),
})
}
#[must_use] #[must_use]
pub fn negate(&self, db: &'db dyn Db) -> Type<'db> { pub fn negate(&self, db: &'db dyn Db) -> Type<'db> {
IntersectionBuilder::new(db).add_negative(*self).build() IntersectionBuilder::new(db).add_negative(*self).build()
@ -5230,16 +5236,12 @@ impl<'db> Type<'db> {
KnownClass::Float.to_instance(db), KnownClass::Float.to_instance(db),
], ],
), ),
_ if class.is_typed_dict(db) => { _ if class.is_typed_dict(db) => Type::typed_dict(*class),
TypedDictType::from(db, ClassType::NonGeneric(*class))
}
_ => Type::instance(db, class.default_specialization(db)), _ => Type::instance(db, class.default_specialization(db)),
}; };
Ok(ty) Ok(ty)
} }
Type::GenericAlias(alias) if alias.is_typed_dict(db) => { Type::GenericAlias(alias) if alias.is_typed_dict(db) => Ok(Type::typed_dict(*alias)),
Ok(TypedDictType::from(db, ClassType::from(*alias)))
}
Type::GenericAlias(alias) => Ok(Type::instance(db, ClassType::from(*alias))), Type::GenericAlias(alias) => Ok(Type::instance(db, ClassType::from(*alias))),
Type::SubclassOf(_) Type::SubclassOf(_)
@ -5562,7 +5564,7 @@ impl<'db> Type<'db> {
Type::AlwaysTruthy | Type::AlwaysFalsy => KnownClass::Type.to_instance(db), Type::AlwaysTruthy | Type::AlwaysFalsy => KnownClass::Type.to_instance(db),
Type::BoundSuper(_) => KnownClass::Super.to_class_literal(db), Type::BoundSuper(_) => KnownClass::Super.to_class_literal(db),
Type::ProtocolInstance(protocol) => protocol.to_meta_type(db), Type::ProtocolInstance(protocol) => protocol.to_meta_type(db),
Type::TypedDict(typed_dict) => SubclassOfType::from(db, typed_dict.defining_class(db)), Type::TypedDict(typed_dict) => SubclassOfType::from(db, typed_dict.defining_class),
} }
} }
@ -5974,7 +5976,7 @@ impl<'db> Type<'db> {
}, },
Type::TypedDict(typed_dict) => { Type::TypedDict(typed_dict) => {
Some(TypeDefinition::Class(typed_dict.defining_class(db).definition(db))) Some(TypeDefinition::Class(typed_dict.defining_class.definition(db)))
} }
Self::Union(_) | Self::Intersection(_) => None, Self::Union(_) | Self::Intersection(_) => None,
@ -9022,24 +9024,16 @@ impl<'db> EnumLiteralType<'db> {
/// Type that represents the set of all inhabitants (`dict` instances) that conform to /// Type that represents the set of all inhabitants (`dict` instances) that conform to
/// a given `TypedDict` schema. /// a given `TypedDict` schema.
#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)] #[derive(Debug, Copy, Clone, PartialEq, Eq, salsa::Update, Hash, get_size2::GetSize)]
#[derive(PartialOrd, Ord)]
pub struct TypedDictType<'db> { pub struct TypedDictType<'db> {
/// A reference to the class (inheriting from `typing.TypedDict`) that specifies the /// A reference to the class (inheriting from `typing.TypedDict`) that specifies the
/// schema of this `TypedDict`. /// schema of this `TypedDict`.
defining_class: ClassType<'db>, defining_class: ClassType<'db>,
} }
// The Salsa heap is tracked separately.
impl get_size2::GetSize for TypedDictType<'_> {}
impl<'db> TypedDictType<'db> { impl<'db> TypedDictType<'db> {
pub(crate) fn from(db: &'db dyn Db, defining_class: ClassType<'db>) -> Type<'db> {
Type::TypedDict(Self::new(db, defining_class))
}
pub(crate) fn items(self, db: &'db dyn Db) -> FxOrderMap<Name, Field<'db>> { pub(crate) fn items(self, db: &'db dyn Db) -> FxOrderMap<Name, Field<'db>> {
let (class_literal, specialization) = self.defining_class(db).class_literal(db); let (class_literal, specialization) = self.defining_class.class_literal(db);
class_literal.fields(db, specialization, CodeGeneratorKind::TypedDict) class_literal.fields(db, specialization, CodeGeneratorKind::TypedDict)
} }
@ -9048,10 +9042,9 @@ impl<'db> TypedDictType<'db> {
db: &'db dyn Db, db: &'db dyn Db,
type_mapping: &TypeMapping<'a, 'db>, type_mapping: &TypeMapping<'a, 'db>,
) -> Self { ) -> Self {
Self::new( Self {
db, defining_class: self.defining_class.apply_type_mapping(db, type_mapping),
self.defining_class(db).apply_type_mapping(db, type_mapping), }
)
} }
} }
@ -9060,7 +9053,7 @@ fn walk_typed_dict_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
typed_dict: TypedDictType<'db>, typed_dict: TypedDictType<'db>,
visitor: &V, visitor: &V,
) { ) {
visitor.visit_type(db, typed_dict.defining_class(db).into()); visitor.visit_type(db, typed_dict.defining_class.into());
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -2922,6 +2922,12 @@ impl<'db> From<ClassLiteral<'db>> for Type<'db> {
} }
} }
impl<'db> From<ClassLiteral<'db>> for ClassType<'db> {
fn from(class: ClassLiteral<'db>) -> ClassType<'db> {
ClassType::NonGeneric(class)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, get_size2::GetSize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, get_size2::GetSize)]
pub(super) enum InheritanceCycle { pub(super) enum InheritanceCycle {
/// The class is cyclically defined and is a participant in the cycle. /// The class is cyclically defined and is a participant in the cycle.

View file

@ -242,9 +242,7 @@ impl Display for DisplayRepresentation<'_> {
} }
f.write_str("]") f.write_str("]")
} }
Type::TypedDict(typed_dict) => { Type::TypedDict(typed_dict) => f.write_str(typed_dict.defining_class.name(self.db)),
f.write_str(typed_dict.defining_class(self.db).name(self.db))
}
} }
} }
} }

View file

@ -11,9 +11,7 @@ use crate::types::cyclic::PairVisitor;
use crate::types::enums::is_single_member_enum; use crate::types::enums::is_single_member_enum;
use crate::types::protocol_class::walk_protocol_interface; use crate::types::protocol_class::walk_protocol_interface;
use crate::types::tuple::{TupleSpec, TupleType}; use crate::types::tuple::{TupleSpec, TupleType};
use crate::types::{ use crate::types::{ClassBase, DynamicType, TypeMapping, TypeRelation, TypeTransformer, UnionType};
ClassBase, DynamicType, TypeMapping, TypeRelation, TypeTransformer, TypedDictType, UnionType,
};
use crate::{Db, FxOrderSet, Program}; use crate::{Db, FxOrderSet, Program};
pub(super) use synthesized_protocol::SynthesizedProtocolType; pub(super) use synthesized_protocol::SynthesizedProtocolType;
@ -34,7 +32,7 @@ impl<'db> Type<'db> {
_ if class_literal.is_protocol(db) => { _ if class_literal.is_protocol(db) => {
Self::ProtocolInstance(ProtocolInstanceType::from_class(class)) Self::ProtocolInstance(ProtocolInstanceType::from_class(class))
} }
_ if class_literal.is_typed_dict(db) => TypedDictType::from(db, class), _ if class_literal.is_typed_dict(db) => Type::typed_dict(class),
_ => Type::non_tuple_instance(class), _ => Type::non_tuple_instance(class),
} }
} }

View file

@ -236,7 +236,9 @@ pub(super) fn union_or_intersection_elements_ordering<'db>(
unreachable!("Two equal, normalized intersections should share the same Salsa ID") unreachable!("Two equal, normalized intersections should share the same Salsa ID")
} }
(Type::TypedDict(left), Type::TypedDict(right)) => left.cmp(right), (Type::TypedDict(left), Type::TypedDict(right)) => {
left.defining_class.cmp(&right.defining_class)
}
(Type::TypedDict(_), _) => Ordering::Less, (Type::TypedDict(_), _) => Ordering::Less,
(_, Type::TypedDict(_)) => Ordering::Greater, (_, Type::TypedDict(_)) => Ordering::Greater,
} }