python / python/mypy

(🐞) False error "statement is unreachable" with `NoReturn` overloaded method

Open
#12,402 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

The overloaded version has different behavior to the normal version. because it is returning NoReturn it should silence the 'unreachable' error.

from typing import overload, NoReturn


@overload
def f(a: str) -> int: ...
@overload
def f(a: str, b: NoReturn) -> NoReturn: ...

def f(a: str, b: NoReturn = ...) -> object: ...

    
b: bool
assert b

if not b:
    f("")  # error: Statement is unreachable ✅
    print("hi")

if not b:
    f("", b)  # error: Statement is unreachable ❌
    print("hi")


def f2(a: str, b: NoReturn) -> NoReturn: ...

if not b:
    f2("", b)  # no error ✅
    print("hi")
> Test run complete: 2 passed, 1 failed <

Failures:
---------------------------
if not b:
    f("", b)  # error: Statement is unreachable ❌
    print("hi")
---------------------------

Expected:
no error ✅

Actual:
error: Statement is unreachable ❌

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

Use the supplied Python reproducer as the starting test case; trace how overloaded calls are checked when one matching signature returns NoReturn. Add a regression test showing that f("", b) does not trigger the unreachable-statement error, then run the relevant mypy tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.