Commit 8019010
authored
Narrow individual items when matching a tuple to a sequence pattern (#16905)
Fixes #12364
When matching a tuple to a sequence pattern, this change narrows the
type of tuple items inside the matched case:
```py
def test(a: bool, b: bool) -> None:
match a, b:
case True, True:
reveal_type(a) # before: "builtins.bool", after: "Literal[True]"
```
This also works with nested tuples, recursively:
```py
def test(a: bool, b: bool, c: bool) -> None:
match a, (b, c):
case _, [True, False]:
reveal_type(c) # before: "builtins.bool", after: "Literal[False]"
```
This only partially fixes issue #12364; see [my comment
there](#12364 (comment))
for more context.
---
This is my first contribution to mypy, so I may miss some context or
conventions; I'm eager for any feedback!
---------
Co-authored-by: Loïc Simon <loic.simon@napta.io>1 parent ec44015 commit 8019010
2 files changed
+83
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5119 | 5119 | | |
5120 | 5120 | | |
5121 | 5121 | | |
| 5122 | + | |
| 5123 | + | |
| 5124 | + | |
5122 | 5125 | | |
5123 | 5126 | | |
5124 | 5127 | | |
| |||
5156 | 5159 | | |
5157 | 5160 | | |
5158 | 5161 | | |
| 5162 | + | |
| 5163 | + | |
| 5164 | + | |
| 5165 | + | |
| 5166 | + | |
| 5167 | + | |
| 5168 | + | |
| 5169 | + | |
| 5170 | + | |
| 5171 | + | |
| 5172 | + | |
| 5173 | + | |
| 5174 | + | |
| 5175 | + | |
5159 | 5176 | | |
5160 | 5177 | | |
5161 | 5178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
344 | 410 | | |
345 | 411 | | |
346 | 412 | | |
| |||
0 commit comments