add canonically_ordered

This commit is contained in:
Douglas Creager 2025-12-13 20:03:15 -05:00
parent 8871fddaf9
commit ddcd76c544
13 changed files with 480 additions and 195 deletions

View File

@ -259,6 +259,12 @@ pub(crate) type NormalizedVisitor<'db> = TypeTransformer<'db, Normalized>;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Normalized; pub(crate) struct Normalized;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum OnlyReorder {
No,
Yes,
}
/// How a generic type has been specialized. /// How a generic type has been specialized.
/// ///
/// This matters only if there is at least one invariant type parameter. /// This matters only if there is at least one invariant type parameter.
@ -563,11 +569,18 @@ impl<'db> PropertyInstanceType<'db> {
Self::new(db, getter, setter) Self::new(db, getter, setter)
} }
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.getter(db).map(|ty| ty.normalized_impl(db, visitor)), self.getter(db)
self.setter(db).map(|ty| ty.normalized_impl(db, visitor)), .map(|ty| ty.normalized_impl(db, only_reorder, visitor)),
self.setter(db)
.map(|ty| ty.normalized_impl(db, only_reorder, visitor)),
) )
} }
@ -1560,56 +1573,75 @@ impl<'db> Type<'db> {
/// - Converts class-based typeddicts into synthesized typeddicts /// - Converts class-based typeddicts into synthesized typeddicts
#[must_use] #[must_use]
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self { pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
}
/// Ensures that all union and intersection elements in this type are in a canonical ordering,
/// but performs no other normalizations.
#[must_use]
pub(crate) fn canonically_ordered(self, db: &'db dyn Db) -> Self {
self.normalized_impl(db, OnlyReorder::Yes, &NormalizedVisitor::default())
} }
#[must_use] #[must_use]
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
Type::Union(union) => visitor.visit(self, || union.normalized_impl(db, visitor)), Type::Union(union) => {
visitor.visit(self, || union.normalized_impl(db, only_reorder, visitor))
}
Type::Intersection(intersection) => visitor.visit(self, || { Type::Intersection(intersection) => visitor.visit(self, || {
Type::Intersection(intersection.normalized_impl(db, visitor)) Type::Intersection(intersection.normalized_impl(db, only_reorder, visitor))
}), }),
Type::Callable(callable) => visitor.visit(self, || { Type::Callable(callable) => visitor.visit(self, || {
Type::Callable(callable.normalized_impl(db, visitor)) Type::Callable(callable.normalized_impl(db, only_reorder, visitor))
}), }),
Type::ProtocolInstance(protocol) => { Type::ProtocolInstance(protocol) => {
visitor.visit(self, || protocol.normalized_impl(db, visitor)) visitor.visit(self, || protocol.normalized_impl(db, only_reorder, visitor))
} }
Type::NominalInstance(instance) => { Type::NominalInstance(instance) => {
visitor.visit(self, || instance.normalized_impl(db, visitor)) visitor.visit(self, || instance.normalized_impl(db, only_reorder, visitor))
} }
Type::FunctionLiteral(function) => visitor.visit(self, || { Type::FunctionLiteral(function) => visitor.visit(self, || {
Type::FunctionLiteral(function.normalized_impl(db, visitor)) Type::FunctionLiteral(function.normalized_impl(db, only_reorder, visitor))
}), }),
Type::PropertyInstance(property) => visitor.visit(self, || { Type::PropertyInstance(property) => visitor.visit(self, || {
Type::PropertyInstance(property.normalized_impl(db, visitor)) Type::PropertyInstance(property.normalized_impl(db, only_reorder, visitor))
}), }),
Type::KnownBoundMethod(method_kind) => visitor.visit(self, || { Type::KnownBoundMethod(method_kind) => visitor.visit(self, || {
Type::KnownBoundMethod(method_kind.normalized_impl(db, visitor)) Type::KnownBoundMethod(method_kind.normalized_impl(db, only_reorder, visitor))
}), }),
Type::BoundMethod(method) => visitor.visit(self, || { Type::BoundMethod(method) => visitor.visit(self, || {
Type::BoundMethod(method.normalized_impl(db, visitor)) Type::BoundMethod(method.normalized_impl(db, only_reorder, visitor))
}), }),
Type::BoundSuper(bound_super) => visitor.visit(self, || { Type::BoundSuper(bound_super) => visitor.visit(self, || {
Type::BoundSuper(bound_super.normalized_impl(db, visitor)) Type::BoundSuper(bound_super.normalized_impl(db, only_reorder, visitor))
}), }),
Type::GenericAlias(generic) => visitor.visit(self, || { Type::GenericAlias(generic) => visitor.visit(self, || {
Type::GenericAlias(generic.normalized_impl(db, visitor)) Type::GenericAlias(generic.normalized_impl(db, only_reorder, visitor))
}), }),
Type::SubclassOf(subclass_of) => visitor.visit(self, || { Type::SubclassOf(subclass_of) => visitor.visit(self, || {
Type::SubclassOf(subclass_of.normalized_impl(db, visitor)) Type::SubclassOf(subclass_of.normalized_impl(db, only_reorder, visitor))
}), }),
Type::TypeVar(bound_typevar) => visitor.visit(self, || { Type::TypeVar(bound_typevar) => visitor.visit(self, || {
Type::TypeVar(bound_typevar.normalized_impl(db, visitor)) Type::TypeVar(bound_typevar.normalized_impl(db, only_reorder, visitor))
}), }),
Type::KnownInstance(known_instance) => visitor.visit(self, || { Type::KnownInstance(known_instance) => visitor.visit(self, || {
Type::KnownInstance(known_instance.normalized_impl(db, visitor)) Type::KnownInstance(known_instance.normalized_impl(db, only_reorder, visitor))
}), }),
Type::TypeIs(type_is) => visitor.visit(self, || { Type::TypeIs(type_is) => visitor.visit(self, || {
type_is.with_type(db, type_is.return_type(db).normalized_impl(db, visitor)) type_is.with_type(
db,
type_is
.return_type(db)
.normalized_impl(db, only_reorder, visitor),
)
}), }),
Type::Dynamic(dynamic) => Type::Dynamic(dynamic.normalized()), Type::Dynamic(dynamic) => Type::Dynamic(dynamic.normalized(only_reorder)),
Type::EnumLiteral(enum_literal) Type::EnumLiteral(enum_literal)
if is_single_member_enum(db, enum_literal.enum_class(db)) => if is_single_member_enum(db, enum_literal.enum_class(db)) =>
{ {
@ -1617,16 +1649,18 @@ impl<'db> Type<'db> {
enum_literal.enum_class_instance(db) enum_literal.enum_class_instance(db)
} }
Type::TypedDict(typed_dict) => visitor.visit(self, || { Type::TypedDict(typed_dict) => visitor.visit(self, || {
Type::TypedDict(typed_dict.normalized_impl(db, visitor)) Type::TypedDict(typed_dict.normalized_impl(db, only_reorder, visitor))
}), }),
Type::TypeAlias(alias) => alias.value_type(db).normalized_impl(db, visitor), Type::TypeAlias(alias) => {
Type::NewTypeInstance(newtype) => { alias
visitor.visit(self, || { .value_type(db)
Type::NewTypeInstance(newtype.map_base_class_type(db, |class_type| { .normalized_impl(db, only_reorder, visitor)
class_type.normalized_impl(db, visitor)
}))
})
} }
Type::NewTypeInstance(newtype) => visitor.visit(self, || {
Type::NewTypeInstance(newtype.map_base_class_type(db, |class_type| {
class_type.normalized_impl(db, only_reorder, visitor)
}))
}),
Type::LiteralString Type::LiteralString
| Type::AlwaysFalsy | Type::AlwaysFalsy
| Type::AlwaysTruthy | Type::AlwaysTruthy
@ -8930,31 +8964,45 @@ impl<'db> VarianceInferable<'db> for KnownInstanceType<'db> {
} }
impl<'db> KnownInstanceType<'db> { impl<'db> KnownInstanceType<'db> {
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
Self::SubscriptedProtocol(context) => { Self::SubscriptedProtocol(context) => {
Self::SubscriptedProtocol(context.normalized_impl(db, visitor)) Self::SubscriptedProtocol(context.normalized_impl(db, only_reorder, visitor))
} }
Self::SubscriptedGeneric(context) => { Self::SubscriptedGeneric(context) => {
Self::SubscriptedGeneric(context.normalized_impl(db, visitor)) Self::SubscriptedGeneric(context.normalized_impl(db, only_reorder, visitor))
}
Self::TypeVar(typevar) => {
Self::TypeVar(typevar.normalized_impl(db, only_reorder, visitor))
} }
Self::TypeVar(typevar) => Self::TypeVar(typevar.normalized_impl(db, visitor)),
Self::TypeAliasType(type_alias) => { Self::TypeAliasType(type_alias) => {
Self::TypeAliasType(type_alias.normalized_impl(db, visitor)) Self::TypeAliasType(type_alias.normalized_impl(db, only_reorder, visitor))
}
Self::Field(field) => Self::Field(field.normalized_impl(db, only_reorder, visitor)),
Self::UnionType(instance) => {
Self::UnionType(instance.normalized_impl(db, only_reorder, visitor))
}
Self::Literal(ty) => Self::Literal(ty.normalized_impl(db, only_reorder, visitor)),
Self::Annotated(ty) => Self::Annotated(ty.normalized_impl(db, only_reorder, visitor)),
Self::TypeGenericAlias(ty) => {
Self::TypeGenericAlias(ty.normalized_impl(db, only_reorder, visitor))
}
Self::Callable(callable) => {
Self::Callable(callable.normalized_impl(db, only_reorder, visitor))
} }
Self::Field(field) => Self::Field(field.normalized_impl(db, visitor)),
Self::UnionType(instance) => Self::UnionType(instance.normalized_impl(db, visitor)),
Self::Literal(ty) => Self::Literal(ty.normalized_impl(db, visitor)),
Self::Annotated(ty) => Self::Annotated(ty.normalized_impl(db, visitor)),
Self::TypeGenericAlias(ty) => Self::TypeGenericAlias(ty.normalized_impl(db, visitor)),
Self::Callable(callable) => Self::Callable(callable.normalized_impl(db, visitor)),
Self::LiteralStringAlias(ty) => { Self::LiteralStringAlias(ty) => {
Self::LiteralStringAlias(ty.normalized_impl(db, visitor)) Self::LiteralStringAlias(ty.normalized_impl(db, only_reorder, visitor))
}
Self::NewType(newtype) => {
Self::NewType(newtype.map_base_class_type(db, |class_type| {
class_type.normalized_impl(db, only_reorder, visitor)
}))
} }
Self::NewType(newtype) => Self::NewType(
newtype
.map_base_class_type(db, |class_type| class_type.normalized_impl(db, visitor)),
),
Self::Deprecated(_) Self::Deprecated(_)
| Self::ConstraintSet(_) | Self::ConstraintSet(_)
| Self::GenericContext(_) | Self::GenericContext(_)
@ -9110,8 +9158,8 @@ pub enum DynamicType<'db> {
} }
impl DynamicType<'_> { impl DynamicType<'_> {
fn normalized(self) -> Self { fn normalized(self, only_reorder: OnlyReorder) -> Self {
if matches!(self, Self::Divergent(_)) { if only_reorder == OnlyReorder::Yes || matches!(self, Self::Divergent(_)) {
self self
} else { } else {
Self::Any Self::Any
@ -9483,11 +9531,16 @@ pub struct FieldInstance<'db> {
impl get_size2::GetSize for FieldInstance<'_> {} impl get_size2::GetSize for FieldInstance<'_> {}
impl<'db> FieldInstance<'db> { impl<'db> FieldInstance<'db> {
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
FieldInstance::new( FieldInstance::new(
db, db,
self.default_type(db) self.default_type(db)
.map(|ty| ty.normalized_impl(db, visitor)), .map(|ty| ty.normalized_impl(db, only_reorder, visitor)),
self.init(db), self.init(db),
self.kw_only(db), self.kw_only(db),
self.alias(db), self.alias(db),
@ -9726,28 +9779,41 @@ impl<'db> TypeVarInstance<'db> {
}) })
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.identity(db), self.identity(db),
self._bound_or_constraints(db) self._bound_or_constraints(db)
.and_then(|bound_or_constraints| match bound_or_constraints { .and_then(|bound_or_constraints| match bound_or_constraints {
TypeVarBoundOrConstraintsEvaluation::Eager(bound_or_constraints) => { TypeVarBoundOrConstraintsEvaluation::Eager(bound_or_constraints) => Some(
Some(bound_or_constraints.normalized_impl(db, visitor).into()) bound_or_constraints
} .normalized_impl(db, only_reorder, visitor)
.into(),
),
TypeVarBoundOrConstraintsEvaluation::LazyUpperBound => self TypeVarBoundOrConstraintsEvaluation::LazyUpperBound => self
.lazy_bound(db) .lazy_bound(db)
.map(|bound| bound.normalized_impl(db, visitor).into()), .map(|bound| bound.normalized_impl(db, only_reorder, visitor).into()),
TypeVarBoundOrConstraintsEvaluation::LazyConstraints => self TypeVarBoundOrConstraintsEvaluation::LazyConstraints => {
.lazy_constraints(db) self.lazy_constraints(db).map(|constraints| {
.map(|constraints| constraints.normalized_impl(db, visitor).into()), constraints
.normalized_impl(db, only_reorder, visitor)
.into()
})
}
}), }),
self.explicit_variance(db), self.explicit_variance(db),
self._default(db).and_then(|default| match default { self._default(db).and_then(|default| match default {
TypeVarDefaultEvaluation::Eager(ty) => Some(ty.normalized_impl(db, visitor).into()), TypeVarDefaultEvaluation::Eager(ty) => {
Some(ty.normalized_impl(db, only_reorder, visitor).into())
}
TypeVarDefaultEvaluation::Lazy => self TypeVarDefaultEvaluation::Lazy => self
.lazy_default(db) .lazy_default(db)
.map(|ty| ty.normalized_impl(db, visitor).into()), .map(|ty| ty.normalized_impl(db, only_reorder, visitor).into()),
}), }),
) )
} }
@ -10454,10 +10520,15 @@ impl<'db> BoundTypeVarInstance<'db> {
}) })
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.typevar(db).normalized_impl(db, visitor), self.typevar(db).normalized_impl(db, only_reorder, visitor),
self.binding_context(db), self.binding_context(db),
self.paramspec_attr(db), self.paramspec_attr(db),
) )
@ -10619,11 +10690,16 @@ impl<'db> TypeVarConstraints<'db> {
} }
} }
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let normalized = self let normalized = self
.elements(db) .elements(db)
.iter() .iter()
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect::<Box<_>>(); .collect::<Box<_>>();
TypeVarConstraints::new(db, normalized) TypeVarConstraints::new(db, normalized)
} }
@ -10663,13 +10739,22 @@ fn walk_type_var_bounds<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
} }
impl<'db> TypeVarBoundOrConstraints<'db> { impl<'db> TypeVarBoundOrConstraints<'db> {
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
TypeVarBoundOrConstraints::UpperBound(bound) => { TypeVarBoundOrConstraints::UpperBound(bound) => TypeVarBoundOrConstraints::UpperBound(
TypeVarBoundOrConstraints::UpperBound(bound.normalized_impl(db, visitor)) bound.normalized_impl(db, only_reorder, visitor),
} ),
TypeVarBoundOrConstraints::Constraints(constraints) => { TypeVarBoundOrConstraints::Constraints(constraints) => {
TypeVarBoundOrConstraints::Constraints(constraints.normalized_impl(db, visitor)) TypeVarBoundOrConstraints::Constraints(constraints.normalized_impl(
db,
only_reorder,
visitor,
))
} }
} }
} }
@ -10850,17 +10935,22 @@ impl<'db> UnionTypeInstance<'db> {
} }
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let value_expr_types = self._value_expr_types(db).as_ref().map(|types| { let value_expr_types = self._value_expr_types(db).as_ref().map(|types| {
types types
.iter() .iter()
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect::<Box<_>>() .collect::<Box<_>>()
}); });
let union_type = self let union_type = self
.union_type(db) .union_type(db)
.clone() .clone()
.map(|ty| ty.normalized_impl(db, visitor)); .map(|ty| ty.normalized_impl(db, only_reorder, visitor));
Self::new(db, value_expr_types, union_type) Self::new(db, value_expr_types, union_type)
} }
@ -10917,8 +11007,16 @@ pub struct InternedType<'db> {
impl get_size2::GetSize for InternedType<'_> {} impl get_size2::GetSize for InternedType<'_> {}
impl<'db> InternedType<'db> { impl<'db> InternedType<'db> {
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
InternedType::new(db, self.inner(db).normalized_impl(db, visitor)) self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
InternedType::new(
db,
self.inner(db).normalized_impl(db, only_reorder, visitor),
)
} }
fn recursive_type_normalized_impl( fn recursive_type_normalized_impl(
@ -12240,11 +12338,17 @@ impl<'db> BoundMethodType<'db> {
) )
} }
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.function(db).normalized_impl(db, visitor), self.function(db).normalized_impl(db, only_reorder, visitor),
self.self_instance(db).normalized_impl(db, visitor), self.self_instance(db)
.normalized_impl(db, only_reorder, visitor),
) )
} }
@ -12454,10 +12558,16 @@ impl<'db> CallableType<'db> {
/// Return a "normalized" version of this `Callable` type. /// Return a "normalized" version of this `Callable` type.
/// ///
/// See [`Type::normalized`] for more details. /// See [`Type::normalized`] for more details.
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
CallableType::new( CallableType::new(
db, db,
self.signatures(db).normalized_impl(db, visitor), self.signatures(db)
.normalized_impl(db, only_reorder, visitor),
self.kind(db), self.kind(db),
) )
} }
@ -12883,19 +12993,40 @@ impl<'db> KnownBoundMethodType<'db> {
} }
} }
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
KnownBoundMethodType::FunctionTypeDunderGet(function) => { KnownBoundMethodType::FunctionTypeDunderGet(function) => {
KnownBoundMethodType::FunctionTypeDunderGet(function.normalized_impl(db, visitor)) KnownBoundMethodType::FunctionTypeDunderGet(function.normalized_impl(
db,
only_reorder,
visitor,
))
} }
KnownBoundMethodType::FunctionTypeDunderCall(function) => { KnownBoundMethodType::FunctionTypeDunderCall(function) => {
KnownBoundMethodType::FunctionTypeDunderCall(function.normalized_impl(db, visitor)) KnownBoundMethodType::FunctionTypeDunderCall(function.normalized_impl(
db,
only_reorder,
visitor,
))
} }
KnownBoundMethodType::PropertyDunderGet(property) => { KnownBoundMethodType::PropertyDunderGet(property) => {
KnownBoundMethodType::PropertyDunderGet(property.normalized_impl(db, visitor)) KnownBoundMethodType::PropertyDunderGet(property.normalized_impl(
db,
only_reorder,
visitor,
))
} }
KnownBoundMethodType::PropertyDunderSet(property) => { KnownBoundMethodType::PropertyDunderSet(property) => {
KnownBoundMethodType::PropertyDunderSet(property.normalized_impl(db, visitor)) KnownBoundMethodType::PropertyDunderSet(property.normalized_impl(
db,
only_reorder,
visitor,
))
} }
KnownBoundMethodType::StrStartswith(_) KnownBoundMethodType::StrStartswith(_)
| KnownBoundMethodType::ConstraintSetRange | KnownBoundMethodType::ConstraintSetRange
@ -13529,7 +13660,12 @@ impl<'db> PEP695TypeAliasType<'db> {
}) })
} }
fn normalized_impl(self, _db: &'db dyn Db, _visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
_db: &'db dyn Db,
_only_reorder: OnlyReorder,
_visitor: &NormalizedVisitor<'db>,
) -> Self {
self self
} }
} }
@ -13586,12 +13722,17 @@ fn walk_manual_pep_695_type_alias<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
} }
impl<'db> ManualPEP695TypeAliasType<'db> { impl<'db> ManualPEP695TypeAliasType<'db> {
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.name(db), self.name(db),
self.definition(db), self.definition(db),
self.value(db).normalized_impl(db, visitor), self.value(db).normalized_impl(db, only_reorder, visitor),
) )
} }
@ -13636,13 +13777,18 @@ fn walk_type_alias_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
} }
impl<'db> TypeAliasType<'db> { impl<'db> TypeAliasType<'db> {
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
TypeAliasType::PEP695(type_alias) => { TypeAliasType::PEP695(type_alias) => {
TypeAliasType::PEP695(type_alias.normalized_impl(db, visitor)) TypeAliasType::PEP695(type_alias.normalized_impl(db, only_reorder, visitor))
} }
TypeAliasType::ManualPEP695(type_alias) => { TypeAliasType::ManualPEP695(type_alias) => {
TypeAliasType::ManualPEP695(type_alias.normalized_impl(db, visitor)) TypeAliasType::ManualPEP695(type_alias.normalized_impl(db, only_reorder, visitor))
} }
} }
} }
@ -13977,21 +14123,26 @@ impl<'db> UnionType<'db> {
/// See [`Type::normalized`] for more details. /// See [`Type::normalized`] for more details.
#[must_use] #[must_use]
pub(crate) fn normalized(self, db: &'db dyn Db) -> Type<'db> { pub(crate) fn normalized(self, db: &'db dyn Db) -> Type<'db> {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
} }
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Type<'db> { ) -> Type<'db> {
self.elements(db) self.elements(db)
.iter() .iter()
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.fold( .fold(
UnionBuilder::new(db) if only_reorder == OnlyReorder::Yes {
.order_elements(true) UnionBuilder::new(db).order_elements(true)
.unpack_aliases(true), } else {
UnionBuilder::new(db)
.order_elements(true)
.unpack_aliases(true)
},
UnionBuilder::add, UnionBuilder::add,
) )
.recursively_defined(self.recursively_defined(db)) .recursively_defined(self.recursively_defined(db))
@ -14170,18 +14321,24 @@ impl<'db> IntersectionType<'db> {
/// See [`Type::normalized`] for more details. /// See [`Type::normalized`] for more details.
#[must_use] #[must_use]
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self { pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
fn normalized_set<'db>( fn normalized_set<'db>(
db: &'db dyn Db, db: &'db dyn Db,
elements: &FxOrderSet<Type<'db>>, elements: &FxOrderSet<Type<'db>>,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> FxOrderSet<Type<'db>> { ) -> FxOrderSet<Type<'db>> {
let mut elements: FxOrderSet<Type<'db>> = elements let mut elements: FxOrderSet<Type<'db>> = elements
.iter() .iter()
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect(); .collect();
elements.sort_unstable_by(|l, r| union_or_intersection_elements_ordering(db, l, r)); elements.sort_unstable_by(|l, r| union_or_intersection_elements_ordering(db, l, r));
@ -14190,8 +14347,8 @@ impl<'db> IntersectionType<'db> {
IntersectionType::new( IntersectionType::new(
db, db,
normalized_set(db, self.positive(db), visitor), normalized_set(db, self.positive(db), only_reorder, visitor),
normalized_set(db, self.negative(db), visitor), normalized_set(db, self.negative(db), only_reorder, visitor),
) )
} }

View File

@ -9,8 +9,8 @@ use crate::{
place::{Place, PlaceAndQualifiers}, place::{Place, PlaceAndQualifiers},
types::{ types::{
ClassBase, ClassType, DynamicType, IntersectionBuilder, KnownClass, MemberLookupPolicy, ClassBase, ClassType, DynamicType, IntersectionBuilder, KnownClass, MemberLookupPolicy,
NominalInstanceType, NormalizedVisitor, SpecialFormType, SubclassOfInner, Type, NominalInstanceType, NormalizedVisitor, OnlyReorder, SpecialFormType, SubclassOfInner,
TypeVarBoundOrConstraints, TypeVarInstance, UnionBuilder, Type, TypeVarBoundOrConstraints, TypeVarInstance, UnionBuilder,
context::InferContext, context::InferContext,
diagnostic::{INVALID_SUPER_ARGUMENT, UNAVAILABLE_IMPLICIT_SUPER_ARGUMENTS}, diagnostic::{INVALID_SUPER_ARGUMENT, UNAVAILABLE_IMPLICIT_SUPER_ARGUMENTS},
todo_type, visitor, todo_type, visitor,
@ -178,14 +178,21 @@ pub enum SuperOwnerKind<'db> {
} }
impl<'db> SuperOwnerKind<'db> { impl<'db> SuperOwnerKind<'db> {
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
SuperOwnerKind::Dynamic(dynamic) => SuperOwnerKind::Dynamic(dynamic.normalized()), SuperOwnerKind::Dynamic(dynamic) => {
SuperOwnerKind::Dynamic(dynamic.normalized(only_reorder))
}
SuperOwnerKind::Class(class) => { SuperOwnerKind::Class(class) => {
SuperOwnerKind::Class(class.normalized_impl(db, visitor)) SuperOwnerKind::Class(class.normalized_impl(db, only_reorder, visitor))
} }
SuperOwnerKind::Instance(instance) => instance SuperOwnerKind::Instance(instance) => instance
.normalized_impl(db, visitor) .normalized_impl(db, only_reorder, visitor)
.as_nominal_instance() .as_nominal_instance()
.map(Self::Instance) .map(Self::Instance)
.unwrap_or(Self::Dynamic(DynamicType::Any)), .unwrap_or(Self::Dynamic(DynamicType::Any)),
@ -606,11 +613,17 @@ impl<'db> BoundSuperType<'db> {
} }
} }
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(super) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.pivot_class(db).normalized_impl(db, visitor), self.pivot_class(db)
self.owner(db).normalized_impl(db, visitor), .normalized_impl(db, only_reorder, visitor),
self.owner(db).normalized_impl(db, only_reorder, visitor),
) )
} }

View File

@ -38,8 +38,8 @@ use crate::types::{
CallableTypes, DATACLASS_FLAGS, DataclassFlags, DataclassParams, DeprecatedInstance, CallableTypes, DATACLASS_FLAGS, DataclassFlags, DataclassParams, DeprecatedInstance,
FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor,
KnownInstanceType, ManualPEP695TypeAliasType, MaterializationKind, NormalizedVisitor, KnownInstanceType, ManualPEP695TypeAliasType, MaterializationKind, NormalizedVisitor,
PropertyInstanceType, StringLiteralType, TypeAliasType, TypeContext, TypeMapping, TypeRelation, OnlyReorder, PropertyInstanceType, StringLiteralType, TypeAliasType, TypeContext, TypeMapping,
TypedDictParams, UnionBuilder, VarianceInferable, binding_type, declaration_type, TypeRelation, TypedDictParams, UnionBuilder, VarianceInferable, binding_type, declaration_type,
determine_upper_bound, determine_upper_bound,
}; };
use crate::{ use crate::{
@ -274,11 +274,17 @@ pub(super) fn walk_generic_alias<'db, V: super::visitor::TypeVisitor<'db> + ?Siz
impl get_size2::GetSize for GenericAlias<'_> {} impl get_size2::GetSize for GenericAlias<'_> {}
impl<'db> GenericAlias<'db> { impl<'db> GenericAlias<'db> {
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(super) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.origin(db), self.origin(db),
self.specialization(db).normalized_impl(db, visitor), self.specialization(db)
.normalized_impl(db, only_reorder, visitor),
) )
} }
@ -442,10 +448,17 @@ impl<'db> ClassType<'db> {
} }
} }
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(super) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
Self::NonGeneric(_) => self, Self::NonGeneric(_) => self,
Self::Generic(generic) => Self::Generic(generic.normalized_impl(db, visitor)), Self::Generic(generic) => {
Self::Generic(generic.normalized_impl(db, only_reorder, visitor))
}
} }
} }

View File

@ -4,8 +4,8 @@ use crate::types::generics::Specialization;
use crate::types::tuple::TupleType; use crate::types::tuple::TupleType;
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, ClassLiteral, ClassType, DynamicType, KnownClass, KnownInstanceType, ApplyTypeMappingVisitor, ClassLiteral, ClassType, DynamicType, KnownClass, KnownInstanceType,
MaterializationKind, MroError, MroIterator, NormalizedVisitor, SpecialFormType, Type, MaterializationKind, MroError, MroIterator, NormalizedVisitor, OnlyReorder, SpecialFormType,
TypeContext, TypeMapping, todo_type, Type, TypeContext, TypeMapping, todo_type,
}; };
/// Enumeration of the possible kinds of types we allow in class bases. /// Enumeration of the possible kinds of types we allow in class bases.
@ -35,10 +35,15 @@ impl<'db> ClassBase<'db> {
Self::Dynamic(DynamicType::Unknown) Self::Dynamic(DynamicType::Unknown)
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
Self::Dynamic(dynamic) => Self::Dynamic(dynamic.normalized()), Self::Dynamic(dynamic) => Self::Dynamic(dynamic.normalized(only_reorder)),
Self::Class(class) => Self::Class(class.normalized_impl(db, visitor)), Self::Class(class) => Self::Class(class.normalized_impl(db, only_reorder, visitor)),
Self::Protocol | Self::Generic | Self::TypedDict => self, Self::Protocol | Self::Generic | Self::TypedDict => self,
} }
} }

View File

@ -83,9 +83,9 @@ use crate::types::{
ApplyTypeMappingVisitor, BoundMethodType, BoundTypeVarInstance, CallableType, CallableTypeKind, ApplyTypeMappingVisitor, BoundMethodType, BoundTypeVarInstance, CallableType, CallableTypeKind,
ClassBase, ClassLiteral, ClassType, DeprecatedInstance, DynamicType, FindLegacyTypeVarsVisitor, ClassBase, ClassLiteral, ClassType, DeprecatedInstance, DynamicType, FindLegacyTypeVarsVisitor,
HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType,
NormalizedVisitor, SpecialFormType, SubclassOfInner, SubclassOfType, Truthiness, Type, NormalizedVisitor, OnlyReorder, SpecialFormType, SubclassOfInner, SubclassOfType, Truthiness,
TypeContext, TypeMapping, TypeRelation, UnionBuilder, binding_type, definition_expression_type, Type, TypeContext, TypeMapping, TypeRelation, UnionBuilder, binding_type,
infer_definition_types, walk_signature, definition_expression_type, infer_definition_types, walk_signature,
}; };
use crate::{Db, FxOrderSet, ModuleName, resolve_module}; use crate::{Db, FxOrderSet, ModuleName, resolve_module};
@ -1156,17 +1156,22 @@ impl<'db> FunctionType<'db> {
} }
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self { pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let literal = self.literal(db); let literal = self.literal(db);
let updated_signature = self let updated_signature = self
.updated_signature(db) .updated_signature(db)
.map(|signature| signature.normalized_impl(db, visitor)); .map(|signature| signature.normalized_impl(db, only_reorder, visitor));
let updated_last_definition_signature = self let updated_last_definition_signature = self
.updated_last_definition_signature(db) .updated_last_definition_signature(db)
.map(|signature| signature.normalized_impl(db, visitor)); .map(|signature| signature.normalized_impl(db, only_reorder, visitor));
Self::new( Self::new(
db, db,
literal, literal,

View File

@ -21,8 +21,8 @@ use crate::types::{
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarIdentity, BoundTypeVarInstance, ApplyTypeMappingVisitor, BindingContext, BoundTypeVarIdentity, BoundTypeVarInstance,
ClassLiteral, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, ClassLiteral, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor,
IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor,
Type, TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarIdentity, OnlyReorder, Type, TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints,
TypeVarInstance, TypeVarKind, TypeVarVariance, UnionType, declaration_type, TypeVarIdentity, TypeVarInstance, TypeVarKind, TypeVarVariance, UnionType, declaration_type,
walk_bound_type_var_type, walk_bound_type_var_type,
}; };
use crate::{Db, FxOrderMap, FxOrderSet}; use crate::{Db, FxOrderMap, FxOrderSet};
@ -658,10 +658,15 @@ impl<'db> GenericContext<'db> {
Specialization::new(db, self, self.fill_in_defaults(db, types), None, None) Specialization::new(db, self, self.fill_in_defaults(db, types), None, None)
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let variables = self let variables = self
.variables(db) .variables(db)
.map(|bound_typevar| bound_typevar.normalized_impl(db, visitor)); .map(|bound_typevar| bound_typevar.normalized_impl(db, only_reorder, visitor));
Self::from_typevar_instances(db, variables) Self::from_typevar_instances(db, variables)
} }
@ -1098,16 +1103,23 @@ impl<'db> Specialization<'db> {
Specialization::new(db, self.generic_context(db), types, None, None) Specialization::new(db, self.generic_context(db), types, None, None)
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let types: Box<[_]> = self let types: Box<[_]> = self
.types(db) .types(db)
.iter() .iter()
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect(); .collect();
let tuple_inner = self let tuple_inner = self
.tuple_inner(db) .tuple_inner(db)
.and_then(|tuple| tuple.normalized_impl(db, visitor)); .and_then(|tuple| tuple.normalized_impl(db, only_reorder, visitor));
let context = self.generic_context(db).normalized_impl(db, visitor); let context = self
.generic_context(db)
.normalized_impl(db, only_reorder, visitor);
Self::new( Self::new(
db, db,
context, context,

View File

@ -14,8 +14,8 @@ use crate::types::protocol_class::{ProtocolClass, walk_protocol_interface};
use crate::types::tuple::{TupleSpec, TupleType, walk_tuple_type}; use crate::types::tuple::{TupleSpec, TupleType, walk_tuple_type};
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, ClassBase, ClassLiteral, FindLegacyTypeVarsVisitor, ApplyTypeMappingVisitor, ClassBase, ClassLiteral, FindLegacyTypeVarsVisitor,
HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, TypeContext, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, OnlyReorder,
TypeMapping, TypeRelation, VarianceInferable, TypeContext, TypeMapping, TypeRelation, VarianceInferable,
}; };
use crate::{Db, FxOrderSet}; use crate::{Db, FxOrderSet};
@ -117,6 +117,7 @@ impl<'db> Type<'db> {
SynthesizedProtocolType::new( SynthesizedProtocolType::new(
db, db,
ProtocolInterface::with_property_members(db, members), ProtocolInterface::with_property_members(db, members),
OnlyReorder::No,
&NormalizedVisitor::default(), &NormalizedVisitor::default(),
), ),
)) ))
@ -369,14 +370,15 @@ impl<'db> NominalInstanceType<'db> {
pub(super) fn normalized_impl( pub(super) fn normalized_impl(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Type<'db> { ) -> Type<'db> {
match self.0 { match self.0 {
NominalInstanceInner::ExactTuple(tuple) => { NominalInstanceInner::ExactTuple(tuple) => {
Type::tuple(tuple.normalized_impl(db, visitor)) Type::tuple(tuple.normalized_impl(db, only_reorder, visitor))
} }
NominalInstanceInner::NonTuple(class) => Type::NominalInstance(NominalInstanceType( NominalInstanceInner::NonTuple(class) => Type::NominalInstance(NominalInstanceType(
NominalInstanceInner::NonTuple(class.normalized_impl(db, visitor)), NominalInstanceInner::NonTuple(class.normalized_impl(db, only_reorder, visitor)),
)), )),
NominalInstanceInner::Object => Type::object(), NominalInstanceInner::Object => Type::object(),
} }
@ -730,7 +732,7 @@ impl<'db> ProtocolInstanceType<'db> {
/// ///
/// See [`Type::normalized`] for more details. /// See [`Type::normalized`] for more details.
pub(super) fn normalized(self, db: &'db dyn Db) -> Type<'db> { pub(super) fn normalized(self, db: &'db dyn Db) -> Type<'db> {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
} }
/// Return a "normalized" version of this `Protocol` type. /// Return a "normalized" version of this `Protocol` type.
@ -739,6 +741,7 @@ impl<'db> ProtocolInstanceType<'db> {
pub(super) fn normalized_impl( pub(super) fn normalized_impl(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Type<'db> { ) -> Type<'db> {
if self.is_equivalent_to_object(db) { if self.is_equivalent_to_object(db) {
@ -746,7 +749,7 @@ impl<'db> ProtocolInstanceType<'db> {
} }
match self.inner { match self.inner {
Protocol::FromClass(_) => Type::ProtocolInstance(Self::synthesized( Protocol::FromClass(_) => Type::ProtocolInstance(Self::synthesized(
SynthesizedProtocolType::new(db, self.inner.interface(db), visitor), SynthesizedProtocolType::new(db, self.inner.interface(db), only_reorder, visitor),
)), )),
Protocol::Synthesized(_) => Type::ProtocolInstance(self), Protocol::Synthesized(_) => Type::ProtocolInstance(self),
} }
@ -909,7 +912,8 @@ mod synthesized_protocol {
use crate::types::protocol_class::ProtocolInterface; use crate::types::protocol_class::ProtocolInterface;
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor,
NormalizedVisitor, Type, TypeContext, TypeMapping, TypeVarVariance, VarianceInferable, NormalizedVisitor, OnlyReorder, Type, TypeContext, TypeMapping, TypeVarVariance,
VarianceInferable,
}; };
use crate::{Db, FxOrderSet}; use crate::{Db, FxOrderSet};
@ -931,9 +935,10 @@ mod synthesized_protocol {
pub(super) fn new( pub(super) fn new(
db: &'db dyn Db, db: &'db dyn Db,
interface: ProtocolInterface<'db>, interface: ProtocolInterface<'db>,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Self { ) -> Self {
Self(interface.normalized_impl(db, visitor)) Self(interface.normalized_impl(db, only_reorder, visitor))
} }
pub(super) fn apply_type_mapping_impl<'a>( pub(super) fn apply_type_mapping_impl<'a>(

View File

@ -15,8 +15,8 @@ use crate::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, CallableType, ClassBase, ClassLiteral, ApplyTypeMappingVisitor, BoundTypeVarInstance, CallableType, ClassBase, ClassLiteral,
ClassType, FindLegacyTypeVarsVisitor, HasRelationToVisitor, ClassType, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
InstanceFallbackShadowsNonDataDescriptor, IsDisjointVisitor, KnownFunction, InstanceFallbackShadowsNonDataDescriptor, IsDisjointVisitor, KnownFunction,
MemberLookupPolicy, NormalizedVisitor, PropertyInstanceType, Signature, Type, TypeMapping, MemberLookupPolicy, NormalizedVisitor, OnlyReorder, PropertyInstanceType, Signature, Type,
TypeQualifiers, TypeRelation, TypeVarVariance, VarianceInferable, TypeMapping, TypeQualifiers, TypeRelation, TypeVarVariance, VarianceInferable,
constraints::{ConstraintSet, IteratorConstraintsExtension, OptionConstraintsExtension}, constraints::{ConstraintSet, IteratorConstraintsExtension, OptionConstraintsExtension},
context::InferContext, context::InferContext,
diagnostic::report_undeclared_protocol_member, diagnostic::report_undeclared_protocol_member,
@ -369,12 +369,22 @@ impl<'db> ProtocolInterface<'db> {
}) })
} }
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(super) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::new( Self::new(
db, db,
self.inner(db) self.inner(db)
.iter() .iter()
.map(|(name, data)| (name.clone(), data.normalized_impl(db, visitor))) .map(|(name, data)| {
(
name.clone(),
data.normalized_impl(db, only_reorder, visitor),
)
})
.collect::<BTreeMap<_, _>>(), .collect::<BTreeMap<_, _>>(),
) )
} }
@ -480,12 +490,17 @@ pub(super) struct ProtocolMemberData<'db> {
impl<'db> ProtocolMemberData<'db> { impl<'db> ProtocolMemberData<'db> {
fn normalized(&self, db: &'db dyn Db) -> Self { fn normalized(&self, db: &'db dyn Db) -> Self {
self.normalized_impl(db, &NormalizedVisitor::default()) self.normalized_impl(db, OnlyReorder::No, &NormalizedVisitor::default())
} }
fn normalized_impl(&self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
&self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self { Self {
kind: self.kind.normalized_impl(db, visitor), kind: self.kind.normalized_impl(db, only_reorder, visitor),
qualifiers: self.qualifiers, qualifiers: self.qualifiers,
} }
} }
@ -593,16 +608,21 @@ enum ProtocolMemberKind<'db> {
} }
impl<'db> ProtocolMemberKind<'db> { impl<'db> ProtocolMemberKind<'db> {
fn normalized_impl(&self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
&self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
ProtocolMemberKind::Method(callable) => { ProtocolMemberKind::Method(callable) => {
ProtocolMemberKind::Method(callable.normalized_impl(db, visitor)) ProtocolMemberKind::Method(callable.normalized_impl(db, only_reorder, visitor))
} }
ProtocolMemberKind::Property(property) => { ProtocolMemberKind::Property(property) => {
ProtocolMemberKind::Property(property.normalized_impl(db, visitor)) ProtocolMemberKind::Property(property.normalized_impl(db, only_reorder, visitor))
} }
ProtocolMemberKind::Other(ty) => { ProtocolMemberKind::Other(ty) => {
ProtocolMemberKind::Other(ty.normalized_impl(db, visitor)) ProtocolMemberKind::Other(ty.normalized_impl(db, only_reorder, visitor))
} }
} }
} }

View File

@ -25,8 +25,8 @@ use crate::types::infer::{infer_deferred_types, infer_scope_types};
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, CallableType, CallableTypeKind, ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, CallableType, CallableTypeKind,
FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor,
KnownClass, MaterializationKind, NormalizedVisitor, ParamSpecAttrKind, TypeContext, KnownClass, MaterializationKind, NormalizedVisitor, OnlyReorder, ParamSpecAttrKind,
TypeMapping, TypeRelation, VarianceInferable, todo_type, TypeContext, TypeMapping, TypeRelation, VarianceInferable, todo_type,
}; };
use crate::{Db, FxOrderSet}; use crate::{Db, FxOrderSet};
use ruff_python_ast::{self as ast, name::Name}; use ruff_python_ast::{self as ast, name::Name};
@ -107,12 +107,13 @@ impl<'db> CallableSignature<'db> {
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
&self, &self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Self { ) -> Self {
Self::from_overloads( Self::from_overloads(
self.overloads self.overloads
.iter() .iter()
.map(|signature| signature.normalized_impl(db, visitor)), .map(|signature| signature.normalized_impl(db, only_reorder, visitor)),
) )
} }
@ -743,12 +744,13 @@ impl<'db> Signature<'db> {
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
&self, &self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Self { ) -> Self {
Self { Self {
generic_context: self generic_context: self
.generic_context .generic_context
.map(|ctx| ctx.normalized_impl(db, visitor)), .map(|ctx| ctx.normalized_impl(db, only_reorder, visitor)),
// Discard the definition when normalizing, so that two equivalent signatures // Discard the definition when normalizing, so that two equivalent signatures
// with different `Definition`s share the same Salsa ID when normalized // with different `Definition`s share the same Salsa ID when normalized
definition: None, definition: None,
@ -756,11 +758,11 @@ impl<'db> Signature<'db> {
db, db,
self.parameters self.parameters
.iter() .iter()
.map(|param| param.normalized_impl(db, visitor)), .map(|param| param.normalized_impl(db, only_reorder, visitor)),
), ),
return_ty: self return_ty: self
.return_ty .return_ty
.map(|return_ty| return_ty.normalized_impl(db, visitor)), .map(|return_ty| return_ty.normalized_impl(db, only_reorder, visitor)),
} }
} }
@ -2133,6 +2135,7 @@ impl<'db> Parameter<'db> {
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
&self, &self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Self { ) -> Self {
let Parameter { let Parameter {
@ -2147,9 +2150,17 @@ impl<'db> Parameter<'db> {
// with a dynamic type as its annotation. (We must use `Any` here as all dynamic types // with a dynamic type as its annotation. (We must use `Any` here as all dynamic types
// normalize to `Any`.) // normalize to `Any`.)
let annotated_type = annotated_type let annotated_type = annotated_type
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.unwrap_or_else(Type::any); .unwrap_or_else(Type::any);
let map_default_type = |ty: Type<'db>| {
if only_reorder == OnlyReorder::Yes {
ty.normalized_impl(db, only_reorder, visitor)
} else {
Type::Never
}
};
// Ensure that parameter names are stripped from positional-only, variadic and keyword-variadic parameters. // Ensure that parameter names are stripped from positional-only, variadic and keyword-variadic parameters.
// Ensure that we only record whether a parameter *has* a default // Ensure that we only record whether a parameter *has* a default
// (strip the precise *type* of the default from the parameter, replacing it with `Never`). // (strip the precise *type* of the default from the parameter, replacing it with `Never`).
@ -2159,17 +2170,17 @@ impl<'db> Parameter<'db> {
default_type, default_type,
} => ParameterKind::PositionalOnly { } => ParameterKind::PositionalOnly {
name: None, name: None,
default_type: default_type.map(|_| Type::Never), default_type: default_type.map(map_default_type),
}, },
ParameterKind::PositionalOrKeyword { name, default_type } => { ParameterKind::PositionalOrKeyword { name, default_type } => {
ParameterKind::PositionalOrKeyword { ParameterKind::PositionalOrKeyword {
name: name.clone(), name: name.clone(),
default_type: default_type.map(|_| Type::Never), default_type: default_type.map(map_default_type),
} }
} }
ParameterKind::KeywordOnly { name, default_type } => ParameterKind::KeywordOnly { ParameterKind::KeywordOnly { name, default_type } => ParameterKind::KeywordOnly {
name: name.clone(), name: name.clone(),
default_type: default_type.map(|_| Type::Never), default_type: default_type.map(map_default_type),
}, },
ParameterKind::Variadic { name: _ } => ParameterKind::Variadic { ParameterKind::Variadic { name: _ } => ParameterKind::Variadic {
name: Name::new_static("args"), name: Name::new_static("args"),

View File

@ -7,8 +7,8 @@ use crate::types::variance::VarianceInferable;
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, ClassType, DynamicType, ApplyTypeMappingVisitor, BoundTypeVarInstance, ClassType, DynamicType,
FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, KnownClass, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor, KnownClass,
MaterializationKind, MemberLookupPolicy, NormalizedVisitor, SpecialFormType, Type, TypeContext, MaterializationKind, MemberLookupPolicy, NormalizedVisitor, OnlyReorder, SpecialFormType, Type,
TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypedDictType, todo_type, TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypedDictType, todo_type,
}; };
use crate::{Db, FxOrderSet}; use crate::{Db, FxOrderSet};
@ -263,9 +263,14 @@ impl<'db> SubclassOfType<'db> {
} }
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self { Self {
subclass_of: self.subclass_of.normalized_impl(db, visitor), subclass_of: self.subclass_of.normalized_impl(db, only_reorder, visitor),
} }
} }
@ -434,12 +439,17 @@ impl<'db> SubclassOfInner<'db> {
Self::TypeVar(bound_typevar) Self::TypeVar(bound_typevar)
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
Self::Class(class) => Self::Class(class.normalized_impl(db, visitor)), Self::Class(class) => Self::Class(class.normalized_impl(db, only_reorder, visitor)),
Self::Dynamic(dynamic) => Self::Dynamic(dynamic.normalized()), Self::Dynamic(dynamic) => Self::Dynamic(dynamic.normalized(only_reorder)),
Self::TypeVar(bound_typevar) => { Self::TypeVar(bound_typevar) => {
Self::TypeVar(bound_typevar.normalized_impl(db, visitor)) Self::TypeVar(bound_typevar.normalized_impl(db, only_reorder, visitor))
} }
} }
} }

View File

@ -29,8 +29,8 @@ use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension};
use crate::types::generics::InferableTypeVars; use crate::types::generics::InferableTypeVars;
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor, ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, Type, TypeMapping, TypeRelation, IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, OnlyReorder, Type, TypeMapping,
UnionBuilder, UnionType, TypeRelation, UnionBuilder, UnionType,
}; };
use crate::types::{Truthiness, TypeContext}; use crate::types::{Truthiness, TypeContext};
use crate::{Db, FxOrderSet, Program}; use crate::{Db, FxOrderSet, Program};
@ -225,9 +225,13 @@ impl<'db> TupleType<'db> {
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Option<Self> { ) -> Option<Self> {
TupleType::new(db, &self.tuple(db).normalized_impl(db, visitor)) TupleType::new(
db,
&self.tuple(db).normalized_impl(db, only_reorder, visitor),
)
} }
pub(super) fn recursive_type_normalized_impl( pub(super) fn recursive_type_normalized_impl(
@ -423,8 +427,17 @@ impl<'db> FixedLengthTuple<Type<'db>> {
} }
#[must_use] #[must_use]
fn normalized_impl(&self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { fn normalized_impl(
Self::from_elements(self.0.iter().map(|ty| ty.normalized_impl(db, visitor))) &self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self::from_elements(
self.0
.iter()
.map(|ty| ty.normalized_impl(db, only_reorder, visitor)),
)
} }
fn recursive_type_normalized_impl( fn recursive_type_normalized_impl(
@ -802,16 +815,21 @@ impl<'db> VariableLengthTuple<Type<'db>> {
} }
#[must_use] #[must_use]
fn normalized_impl(&self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> TupleSpec<'db> { fn normalized_impl(
&self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> TupleSpec<'db> {
let prefix = self let prefix = self
.prenormalized_prefix_elements(db, None) .prenormalized_prefix_elements(db, None)
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect::<Box<_>>(); .collect::<Box<_>>();
let suffix = self let suffix = self
.prenormalized_suffix_elements(db, None) .prenormalized_suffix_elements(db, None)
.map(|ty| ty.normalized_impl(db, visitor)) .map(|ty| ty.normalized_impl(db, only_reorder, visitor))
.collect::<Box<_>>(); .collect::<Box<_>>();
let variable = self.variable.normalized_impl(db, visitor); let variable = self.variable.normalized_impl(db, only_reorder, visitor);
TupleSpec::Variable(Self { TupleSpec::Variable(Self {
prefix, prefix,
variable, variable,
@ -1280,11 +1298,12 @@ impl<'db> Tuple<Type<'db>> {
pub(crate) fn normalized_impl( pub(crate) fn normalized_impl(
&self, &self,
db: &'db dyn Db, db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>, visitor: &NormalizedVisitor<'db>,
) -> Self { ) -> Self {
match self { match self {
Tuple::Fixed(tuple) => Tuple::Fixed(tuple.normalized_impl(db, visitor)), Tuple::Fixed(tuple) => Tuple::Fixed(tuple.normalized_impl(db, only_reorder, visitor)),
Tuple::Variable(tuple) => tuple.normalized_impl(db, visitor), Tuple::Variable(tuple) => tuple.normalized_impl(db, only_reorder, visitor),
} }
} }

View File

@ -29,13 +29,13 @@ pub(super) fn union_or_intersection_elements_ordering<'db>(
) -> Ordering { ) -> Ordering {
debug_assert_eq!( debug_assert_eq!(
*left, *left,
left.normalized(db), left.canonically_ordered(db),
"`left` must be normalized before a meaningful ordering can be established" "`left` must be canonically ordered before a meaningful ordering can be established"
); );
debug_assert_eq!( debug_assert_eq!(
*right, *right,
right.normalized(db), right.canonically_ordered(db),
"`right` must be normalized before a meaningful ordering can be established" "`right` must be canonically ordered before a meaningful ordering can be established"
); );
if left == right { if left == right {

View File

@ -21,8 +21,8 @@ use crate::types::class::FieldKind;
use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension}; use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension};
use crate::types::generics::InferableTypeVars; use crate::types::generics::InferableTypeVars;
use crate::types::{ use crate::types::{
HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, TypeContext, HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, NormalizedVisitor, OnlyReorder,
TypeRelation, TypeContext, TypeRelation,
}; };
use ordermap::OrderSet; use ordermap::OrderSet;
@ -298,14 +298,19 @@ impl<'db> TypedDictType<'db> {
} }
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
match self { match self {
TypedDictType::Class(_) => { TypedDictType::Class(_) => {
let synthesized = SynthesizedTypedDictType::new(db, self.items(db)); let synthesized = SynthesizedTypedDictType::new(db, self.items(db));
TypedDictType::Synthesized(synthesized.normalized_impl(db, visitor)) TypedDictType::Synthesized(synthesized.normalized_impl(db, only_reorder, visitor))
} }
TypedDictType::Synthesized(synthesized) => { TypedDictType::Synthesized(synthesized) => {
TypedDictType::Synthesized(synthesized.normalized_impl(db, visitor)) TypedDictType::Synthesized(synthesized.normalized_impl(db, only_reorder, visitor))
} }
} }
} }
@ -768,12 +773,17 @@ impl<'db> SynthesizedTypedDictType<'db> {
SynthesizedTypedDictType::new(db, items) SynthesizedTypedDictType::new(db, items)
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
let items = self let items = self
.items(db) .items(db)
.iter() .iter()
.map(|(name, field)| { .map(|(name, field)| {
let field = field.clone().normalized_impl(db, visitor); let field = field.clone().normalized_impl(db, only_reorder, visitor);
(name.clone(), field) (name.clone(), field)
}) })
.collect::<TypedDictSchema<'db>>(); .collect::<TypedDictSchema<'db>>();
@ -845,9 +855,14 @@ impl<'db> TypedDictField<'db> {
} }
} }
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self { pub(crate) fn normalized_impl(
self,
db: &'db dyn Db,
only_reorder: OnlyReorder,
visitor: &NormalizedVisitor<'db>,
) -> Self {
Self { Self {
declared_ty: self.declared_ty.normalized_impl(db, visitor), declared_ty: self.declared_ty.normalized_impl(db, only_reorder, visitor),
flags: self.flags, flags: self.flags,
// A normalized typed-dict field does not hold onto the original declaration, // A normalized typed-dict field does not hold onto the original declaration,
// since a normalized typed-dict is an abstract type where equality does not depend // since a normalized typed-dict is an abstract type where equality does not depend