Skip to content

NoReturn should declare the error which raises #145660

@BarbTurnip437

Description

@BarbTurnip437

Feature or enhancement

Proposal:

from typing import NoReturn


def divide(a: int, b: int) -> float | NoReturn:
    return a/b


if __name__ == "__main__":
    try:
        print(divide(1, 0))
    except ValueError:
        print("err")

In this piece of code, the type checker won't really see anything wrong,
but the divide function actually raises a ZeroDivisionError,
so the code will actually go wrong.

from typing import NoReturn


def divide(a: int, b: int) -> float | NoReturn[ZeroDivisionError]:
    return a/b


if __name__ == "__main__":
    try:
        print(divide(1, 0))
    except ValueError:
        print("err")

By making NoReturn actually declaring the error which raises,
type checkers could actually see that you're not handling ZeroDivisionError,
so you can spot the error before runtime.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions