Commit 7959a20
authored
Suppress errors for unreachable branches in conditional expressions (#18295)
Fixes #4134
Fixes #9195
Suppress errors when analyzing unreachable conditional expression
branches. Same idea as what's done when analyzing the right-hand operand
of `and`/`or`:
https://github.com/python/mypy/blob/973618a6bfa88398e08dc250c8427b381b3a0fce/mypy/checkexpr.py#L4252-L4256
This PR originally added filters of the same form to the places where
`analyze_cond_branch` is called in
`ExpressionChecker.visit_conditional_expr`. However, since 5 out of the
6 `analyze_cond_branch` call sites now use `filter_errors` for the case
when `map is None`, I decided to move the error filtering logic to
inside `analyze_cond_branch`.
**Given:**
```python
from typing import TypeVar
T = TypeVar("T", int, str)
def foo(x: T) -> T:
return x + 1 if isinstance(x, int) else x + "a"
```
**Before:**
```none
main.py:5:16: error: Unsupported operand types for + ("str" and "int") [operator]
main.py:5:49: error: Unsupported operand types for + ("int" and "str") [operator]
Found 2 errors in 1 file (checked 1 source file)
```
**After:**
```
Success: no issues found in 1 source file
```1 parent d33cef8 commit 7959a20
File tree
3 files changed
+16
-8
lines changed- mypy
- test-data/unit
3 files changed
+16
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4793 | 4793 | | |
4794 | 4794 | | |
4795 | 4795 | | |
4796 | | - | |
| 4796 | + | |
| 4797 | + | |
| 4798 | + | |
4797 | 4799 | | |
4798 | 4800 | | |
4799 | 4801 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4249 | 4249 | | |
4250 | 4250 | | |
4251 | 4251 | | |
4252 | | - | |
4253 | | - | |
4254 | | - | |
4255 | | - | |
4256 | | - | |
| 4252 | + | |
4257 | 4253 | | |
4258 | 4254 | | |
4259 | 4255 | | |
| |||
5851 | 5847 | | |
5852 | 5848 | | |
5853 | 5849 | | |
| 5850 | + | |
5854 | 5851 | | |
5855 | 5852 | | |
5856 | 5853 | | |
5857 | 5854 | | |
5858 | | - | |
5859 | | - | |
| 5855 | + | |
| 5856 | + | |
| 5857 | + | |
| 5858 | + | |
5860 | 5859 | | |
5861 | 5860 | | |
5862 | 5861 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1506 | 1506 | | |
1507 | 1507 | | |
1508 | 1508 | | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
1509 | 1516 | | |
1510 | 1517 | | |
1511 | 1518 | | |
| |||
0 commit comments