@@ -150,7 +150,81 @@ r: dict[int | str, int | str] = {1: 1, 2: 2, 3: 3}
150150reveal_type(r) # revealed: dict[int | str, int | str]
151151```
152152
153- ## Incorrect collection literal assignments are complained aobut
153+ ## Optional collection literal annotations are understood
154+
155+ ``` toml
156+ [environment ]
157+ python-version = " 3.12"
158+ ```
159+
160+ ``` py
161+ import typing
162+
163+ a: list[int ] | None = [1 , 2 , 3 ]
164+ reveal_type(a) # revealed: list[int]
165+
166+ b: list[int | str ] | None = [1 , 2 , 3 ]
167+ reveal_type(b) # revealed: list[int | str]
168+
169+ c: typing.List[int ] | None = [1 , 2 , 3 ]
170+ reveal_type(c) # revealed: list[int]
171+
172+ d: list[typing.Any] | None = []
173+ reveal_type(d) # revealed: list[Any]
174+
175+ e: set[int ] | None = {1 , 2 , 3 }
176+ reveal_type(e) # revealed: set[int]
177+
178+ f: set[int | str ] | None = {1 , 2 , 3 }
179+ reveal_type(f) # revealed: set[int | str]
180+
181+ g: typing.Set[int ] | None = {1 , 2 , 3 }
182+ reveal_type(g) # revealed: set[int]
183+
184+ h: list[list[int ]] | None = [[], [42 ]]
185+ reveal_type(h) # revealed: list[list[int]]
186+
187+ i: list[typing.Any] | None = [1 , 2 , " 3" , ([4 ],)]
188+ reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]]
189+
190+ j: list[tuple[str | int , ... ]] | None = [(1 , 2 ), (" foo" , " bar" ), ()]
191+ reveal_type(j) # revealed: list[tuple[str | int, ...]]
192+
193+ k: list[tuple[list[int ], ... ]] | None = [([],), ([1 , 2 ], [3 , 4 ]), ([5 ], [6 ], [7 ])]
194+ reveal_type(k) # revealed: list[tuple[list[int], ...]]
195+
196+ l: tuple[list[int ], * tuple[list[typing.Any], ... ], list[str ]] | None = ([1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ], [" 10" , " 11" , " 12" ])
197+ # TODO : this should be `tuple[list[int], list[Any | int], list[Any | int], list[str]]`
198+ reveal_type(l) # revealed: tuple[list[Unknown | int], list[Unknown | int], list[Unknown | int], list[Unknown | str]]
199+
200+ type IntList = list[int ]
201+
202+ m: IntList | None = [1 , 2 , 3 ]
203+ reveal_type(m) # revealed: list[int]
204+
205+ # TODO : this should type-check and avoid literal promotion
206+ # error: [invalid-assignment] "Object of type `list[Unknown | int]` is not assignable to `list[Literal[1, 2, 3]] | None`"
207+ n: list[typing.Literal[1 , 2 , 3 ]] | None = [1 , 2 , 3 ]
208+ # TODO : this should be `list[Literal[1, 2, 3]]` at this scope
209+ reveal_type(n) # revealed: list[Literal[1, 2, 3]] | None
210+
211+ # TODO : this should type-check and avoid literal promotion
212+ # error: [invalid-assignment] "Object of type `list[Unknown | str]` is not assignable to `list[LiteralString] | None`"
213+ o: list[typing.LiteralString] | None = [" a" , " b" , " c" ]
214+ # TODO : this should be `list[LiteralString]` at this scope
215+ reveal_type(o) # revealed: list[LiteralString] | None
216+
217+ p: dict[int , int ] | None = {}
218+ reveal_type(p) # revealed: dict[int, int]
219+
220+ q: dict[int | str , int ] | None = {1 : 1 , 2 : 2 , 3 : 3 }
221+ reveal_type(q) # revealed: dict[int | str, int]
222+
223+ r: dict[int | str , int | str ] | None = {1 : 1 , 2 : 2 , 3 : 3 }
224+ reveal_type(r) # revealed: dict[int | str, int | str]
225+ ```
226+
227+ ## Incorrect collection literal assignments are complained about
154228
155229``` py
156230# error: [invalid-assignment] "Object of type `list[Unknown | int]` is not assignable to `list[str]`"
0 commit comments