Skip to content

Commit ec3b8c7

Browse files
committed
use fallback instance type for literal list elements
1 parent e13b1af commit ec3b8c7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

crates/ty_python_semantic/resources/mdtest/assignment/annotations.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ reveal_type(g) # revealed: set[int]
112112

113113
h: list[list[int]] = [[], [42]]
114114
# TODO: revealed: list[list[int]]
115-
reveal_type(h) # revealed: list[list[int] | list[Unknown] | list[Unknown | Literal[42]]]
115+
reveal_type(h) # revealed: list[list[int] | list[Unknown] | list[Unknown | int]]
116116

117117
i: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()]
118118
reveal_type(i) # revealed: list[tuple[str | int, ...]]
119119

120120
j: list[tuple[list[typing.Any], ...]] = [([],), ([1, 2], [3, 4]), (["foo"], ["bar"])]
121121
# TODO: revealed: list[tuple[list[typing.Any], ...]]
122-
# revealed: list[tuple[list[Any], ...] | tuple[list[Unknown]] | tuple[list[Unknown | Literal[1, 2]], list[Unknown | Literal[3, 4]]] | tuple[list[Unknown | Literal["foo"]], list[Unknown | Literal["bar"]]]]
122+
# revealed: list[tuple[list[Any], ...] | tuple[list[Unknown]] | tuple[list[Unknown | int], list[Unknown | int]] | tuple[list[Unknown | str], list[Unknown | str]]]
123123
reveal_type(j)
124124

125125
type IntList = list[int]
@@ -131,10 +131,10 @@ reveal_type(a) # revealed: list[int]
131131
## Incorrect collection literal assignments are complained aobut
132132

133133
```py
134-
# error: [invalid-assignment] "Object of type `list[Literal[1, 2, 3]]` is not assignable to `list[str]`"
134+
# error: [invalid-assignment] "Object of type `list[int]` is not assignable to `list[str]`"
135135
a: list[str] = [1, 2, 3]
136136

137-
# error: [invalid-assignment] "Object of type `set[Literal[1, 2, "3"]]` is not assignable to `set[int]`"
137+
# error: [invalid-assignment] "Object of type `set[int | str]` is not assignable to `set[int]`"
138138
b: set[int] = {1, 2, "3"}
139139
```
140140

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5366,6 +5366,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
53665366

53675367
// The inferred type of each element acts as an additional constraint on `T`.
53685368
for inferred_elt_ty in inferred_elt_tys {
5369+
// Use the fallback instance type for element literals to avoid excessively large unions
5370+
// for large nested list literals, which the constraint solver struggles with.
5371+
let inferred_elt_ty = inferred_elt_ty
5372+
.literal_fallback_instance(self.db())
5373+
.unwrap_or(inferred_elt_ty);
5374+
53695375
builder.infer(elts_ty, inferred_elt_ty).ok()?;
53705376
}
53715377

0 commit comments

Comments
 (0)