False positive unreachable with unrelated types in match-case statement
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
When type in a class pattern of a `case` branch is unrelated to the type of the scrutinee, pyright reports the branch as unreachable. That’s incorrect, because a common subclass of both types can exist.
**Code or Screenshots**
This code prints `reached` in runtime, but pyright reports the branch as unreachable and infers the type of `t` as `Never` instead of correct “subclass of `Foo` and `Bar`” as [mypy does](https://mypy-play.net/?mypy=latest&python=3.12&gist=4f94c718861b7f6a9182381a3d95b24c).
```python
class Foo:
pass
class Bar:
pass
class Baz(Foo, Bar):
pass
def check(x: Foo) -> None:
match x:
case Bar() as t:
print('reached', t)
check(Baz())
```
**VS Code extension or command-line**
I’m running pyright in Neovim, but this also reproduces [on playground](https://pyright-play.net/?pythonVersion=3.13&code=MYGwhgzhAEBiD28BcAoa7oAdIRS0O0AQmAE6oZY54FTFgBeAFAvADT2kCUFG2UeACYBTAGbRgAC2HAA1kwAeSOIi7QAtAD5oAOXgA7Yb3QBbMABcp0JWkoZgkYZyZrI0c8buVMpAJb7zJgByUmEwKWFBII5zLhppOSYSZi44oA).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked pyright playground reproduction and trace the match-case reachability and type-narrowing diagnostics it produces. Confirm the fix when the branch is no longer reported as unreachable and t is inferred as a possible subclass of both Foo and Bar, while the example still runs unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100