sort union types originating from recursive queries, even outside of cycle recovery functions

This commit is contained in:
Shunsuke Shibayama 2025-12-01 22:36:05 +09:00
parent 56d3173da5
commit 42448fdba9
4 changed files with 6 additions and 6 deletions

View File

@ -2382,7 +2382,7 @@ class B:
self.x = other.x
reveal_type(B().x) # revealed: Literal[1] | Unknown
reveal_type(A().x) # revealed: Unknown | Literal[1]
reveal_type(A().x) # revealed: Literal[1] | Unknown
class Base:
def flip(self) -> "Sub":
@ -2674,7 +2674,7 @@ class C:
self.x = (other.x, 1)
reveal_type(C().x) # revealed: tuple[Divergent, Literal[1]] | Unknown
reveal_type(C().x[0]) # revealed: Divergent | Unknown
reveal_type(C().x[0]) # revealed: Unknown | Divergent
```
This also works if the tuple is not constructed directly:

View File

@ -29,7 +29,7 @@ class Point:
p = Point()
reveal_type(p.x) # revealed: int | Unknown
reveal_type(p.y) # revealed: Unknown | int
reveal_type(p.y) # revealed: int | Unknown
```
## Self-referential bare type alias

View File

@ -189,7 +189,7 @@ r5: RecursiveList[int] = [1, ["a"]]
def _(x: RecursiveList[int]):
if isinstance(x, list):
# TODO: should be `list[RecursiveList[int]]
reveal_type(x[0]) # revealed: list[Any] | int
reveal_type(x[0]) # revealed: int | list[Any]
if isinstance(x, list) and isinstance(x[0], list):
# TODO: should be `list[RecursiveList[int]]`
reveal_type(x[0]) # revealed: list[Any]

View File

@ -593,7 +593,7 @@ impl<'db> UnionBuilder<'db> {
// 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.cycle_recovery && self.recursively_defined.is_yes() {
if self.recursively_defined.is_yes() {
self.order_elements = true;
}
let mut types = vec![];
@ -613,7 +613,7 @@ impl<'db> UnionBuilder<'db> {
}
if self.order_elements {
types.sort_unstable_by(|l, r| {
if self.cycle_recovery && self.recursively_defined.is_yes() {
if self.recursively_defined.is_yes() {
structural_type_ordering(self.db, l, r)
} else {
union_or_intersection_elements_ordering(self.db, l, r)