microsoft / microsoft/pyright

False positive unreachable with unrelated types in match-case statement

Open
#10,329 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs decision
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.