From b575dcd6b87366b32c7d10fbaf2096f158c6bfe6 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Fri, 12 Dec 2025 15:18:35 +0900 Subject: [PATCH] Revert "experiment: sort all union" This reverts commit 7c89918f7f61a81c151081b79f9c8fb40265b7d9. --- crates/ty_python_semantic/src/types/builder.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/ty_python_semantic/src/types/builder.rs b/crates/ty_python_semantic/src/types/builder.rs index 6302094fa3..26de65a12a 100644 --- a/crates/ty_python_semantic/src/types/builder.rs +++ b/crates/ty_python_semantic/src/types/builder.rs @@ -629,7 +629,13 @@ impl<'db> UnionBuilder<'db> { self.try_build().unwrap_or(Type::Never) } - pub(crate) fn try_build(self) -> Option> { + pub(crate) fn try_build(mut self) -> Option> { + // If the type is defined recursively, the union type is sorted and normalized. + // This is because the execution order of the queries is not deterministic and may result in a different order of elements. + // The order of the union type does not affect the type check result, but unstable output is undesirable. + if self.recursively_defined.is_yes() { + self.order_elements = true; + } let mut types = vec![]; for element in self.elements { match element { @@ -653,8 +659,6 @@ impl<'db> UnionBuilder<'db> { union_or_intersection_elements_ordering(self.db, l, r) } }); - } else { - types.sort_unstable_by(|l, r| structural_type_ordering(self.db, l, r)); } match types.len() { 0 => None,