diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index c5392fccdd..94c8d0b47d 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -14194,7 +14194,7 @@ impl<'db> UnionType<'db> { nested: bool, ) -> Option> { let mut builder = UnionBuilder::new(db) - .order_elements(true) + .order_elements(false) .unpack_aliases(false) .cycle_recovery(true) .recursively_defined(self.recursively_defined(db)); diff --git a/crates/ty_python_semantic/src/types/builder.rs b/crates/ty_python_semantic/src/types/builder.rs index 21aa447b26..26de65a12a 100644 --- a/crates/ty_python_semantic/src/types/builder.rs +++ b/crates/ty_python_semantic/src/types/builder.rs @@ -38,6 +38,7 @@ //! unnecessary `is_subtype_of` checks. use crate::types::enums::{enum_member_literals, enum_metadata}; +use crate::types::type_ordering::union_or_intersection_elements_ordering; use crate::types::{ BytesLiteralType, IntersectionType, KnownClass, StringLiteralType, Type, TypeVarBoundOrConstraints, UnionType, structural_type_ordering, @@ -651,7 +652,13 @@ impl<'db> UnionBuilder<'db> { } } if self.order_elements { - types.sort_unstable_by(|l, r| structural_type_ordering(self.db, l, r)); + types.sort_unstable_by(|l, r| { + if self.recursively_defined.is_yes() { + structural_type_ordering(self.db, l, r) + } else { + union_or_intersection_elements_ordering(self.db, l, r) + } + }); } match types.len() { 0 => None,