microsoft / microsoft/pyright

Avoid triggering reportUnreachable on exception raising?

Open
#11,041 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement request
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

(Acknowledging that this is a new and disabled-by-default rule)

Currently pyright will trigger reportUnreachable on the following code:

def foo(x: int | str):
    if isinstance(x, int):
        print("int")
    elif isinstance(x, str):
        print("str")
    else:
        raise ValueError  # error

mypy's version of the same rule ignores unreachable statements that are of a small set of forms, like raise (expr) or assert False, or statements are equivalent to a correctly used assert_never: https://github.com/python/mypy/blob/master/mypy/checker.py#L3139-L3165

This makes these diagnostics substantially higher signal

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 pyright's reportUnreachable handling and compare it with the linked mypy checker.py logic for ignored unreachable forms. Use the provided isinstance example to verify that the raise statement no longer produces a diagnostic while other unreachable statements still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.