Commit 8bbdd30
committed
Fix narrowing in comprehensions when the intersection is impossible
When a comprehension condition narrows the index variable to an
impossible type (e.g. isinstance/issubclass where mypy determines a
subclass of both classes cannot exist, or the class is @Final),
check_for_comp() pushed an unreachable type map, which just marked the
binder frame unreachable without recording any narrowing. Unlike
statements, the comprehension's left expression is still type checked,
so it was checked with the *unnarrowed* type, producing false positives
like:
error: List comprehension has incompatible type List[type[A]];
expected List[type[M]]
Apply the impossible narrowing to the binder instead, so the left
expression checks against Never, matching the narrowing an equivalent
if statement would produce.
Fixes #216351 parent 1462b4e commit 8bbdd30
2 files changed
Lines changed: 40 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6040 | 6040 | | |
6041 | 6041 | | |
6042 | 6042 | | |
6043 | | - | |
| 6043 | + | |
| 6044 | + | |
| 6045 | + | |
| 6046 | + | |
| 6047 | + | |
| 6048 | + | |
| 6049 | + | |
| 6050 | + | |
| 6051 | + | |
6044 | 6052 | | |
6045 | 6053 | | |
6046 | 6054 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1614 | 1614 | | |
1615 | 1615 | | |
1616 | 1616 | | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
1617 | 1648 | | |
1618 | 1649 | | |
1619 | 1650 | | |
| |||
0 commit comments