|
1 | 1 | # Unpacking |
2 | 2 |
|
3 | | -If there are not enough or too many values when unpacking, an error will occur and the types of |
4 | | -all variables (if nested tuple unpacking fails, only the variables within the failed tuples) is |
5 | | -inferred to be `Unknown`. |
| 3 | +If there are not enough or too many values when unpacking, an error will occur and the types of all |
| 4 | +variables (if nested tuple unpacking fails, only the variables within the failed tuples) is inferred |
| 5 | +to be `Unknown`. |
6 | 6 |
|
7 | 7 | ## Tuple |
8 | 8 |
|
@@ -207,6 +207,57 @@ reveal_type(c) # revealed: int |
207 | 207 | reveal_type(d) # revealed: Literal[2] |
208 | 208 | ``` |
209 | 209 |
|
| 210 | +## List |
| 211 | + |
| 212 | +### Literal unpacking |
| 213 | + |
| 214 | +```py |
| 215 | +a, b = [1, 2] |
| 216 | +# TODO: should be `int` for both `a` and `b` |
| 217 | +reveal_type(a) # revealed: Unknown |
| 218 | +reveal_type(b) # revealed: Unknown |
| 219 | +``` |
| 220 | + |
| 221 | +### Simple unpacking |
| 222 | + |
| 223 | +```py |
| 224 | +def _(value: list[int]): |
| 225 | + a, b = value |
| 226 | + reveal_type(a) # revealed: int |
| 227 | + reveal_type(b) # revealed: int |
| 228 | +``` |
| 229 | + |
| 230 | +### Nested unpacking |
| 231 | + |
| 232 | +```py |
| 233 | +def _(value: list[list[int]]): |
| 234 | + a, (b, c) = value |
| 235 | + reveal_type(a) # revealed: list[int] |
| 236 | + reveal_type(b) # revealed: int |
| 237 | + reveal_type(c) # revealed: int |
| 238 | +``` |
| 239 | + |
| 240 | +### Invalid nested unpacking |
| 241 | + |
| 242 | +```py |
| 243 | +def _(value: list[int]): |
| 244 | + # error: [not-iterable] "Object of type `int` is not iterable" |
| 245 | + a, (b, c) = value |
| 246 | + reveal_type(a) # revealed: int |
| 247 | + reveal_type(b) # revealed: Unknown |
| 248 | + reveal_type(c) # revealed: Unknown |
| 249 | +``` |
| 250 | + |
| 251 | +### Starred expression |
| 252 | + |
| 253 | +```py |
| 254 | +def _(value: list[int]): |
| 255 | + a, *b, c = value |
| 256 | + reveal_type(a) # revealed: int |
| 257 | + reveal_type(b) # revealed: list[int] |
| 258 | + reveal_type(c) # revealed: int |
| 259 | +``` |
| 260 | + |
210 | 261 | ## String |
211 | 262 |
|
212 | 263 | ### Simple unpacking |
@@ -293,6 +344,18 @@ reveal_type(b) # revealed: LiteralString |
293 | 344 | reveal_type(c) # revealed: list[LiteralString] |
294 | 345 | ``` |
295 | 346 |
|
| 347 | +### Starred expression (6) |
| 348 | + |
| 349 | +```py |
| 350 | +from typing_extensions import LiteralString |
| 351 | + |
| 352 | +def _(s: LiteralString): |
| 353 | + a, b, *c = s |
| 354 | + reveal_type(a) # revealed: LiteralString |
| 355 | + reveal_type(b) # revealed: LiteralString |
| 356 | + reveal_type(c) # revealed: list[LiteralString] |
| 357 | +``` |
| 358 | + |
296 | 359 | ### Unicode |
297 | 360 |
|
298 | 361 | ```py |
|
0 commit comments