mypy reports false-positive [exhaustive-match] error when there is an unreachable match
bugtopic-match-statementtopic-reachability
**Bug Report**
`exhaustive-match` incorrectly reports a missing `A` case when using a capture pattern (`case A() as a`) together with an unreachable extra branch.
The following code reports:
```text
error: Match statement has unhandled case for values of type "A" [exhaustive-match]
```
even though all possible values of `A | None` are handled.
Interestingly, the error disappears if the unreachable `case B():` branch is removed.
This seems related to exhaustiveness analysis for class patterns with `as` captures.
**To Reproduce**
```python
# mypy: enable-error-code=exhaustive-match
class A: pass
class B: pass
x: A | None
match x:
case None:
pass
case A() as a:
pass
case B():
pass
```
**Expected Behavior**
No errors should be reported.
`x` has type `A | None`, and both `None` and `A` are handled exhaustively.
The `case B():` branch is unreachable and should not affect exhaustiveness checking.
Removing the `case B():` branch currently makes mypy report no errors:
```python
match x:
case None:
pass
case A() as a:
pass
```
**Actual Behavior**
```text
main.py:8: error: Match statement has unhandled case for values of type "A" [exhaustive-match]
main.py:8: note: If match statement is intended to be non-exhaustive, add `case _: pass`
Found 1 error in 1 file (checked 1 source file)
```
**Your Environment**
- Mypy version used: 2.1.0
- Mypy command-line flags: `--enable-error-code exhaustive-match`
- Mypy configuration options from `mypy.ini` (and other config files): none
- Python version used: 3.13
- see https://mypy-play.net/?mypy=2.1.0&python=3.13&enable-error-code=exhaustive-match&gist=027087d73156a9153fe92b49b19c9ce9
0 条评论