Skip to content

NoReturn type incorrectly used in some functions. #2439

@stitova-idm

Description

@stitova-idm

This doesn't cause an issue during run-time, but if you call these functions in your code, the code after them is highlighted as UNREACHEABLE in PyCharm.

Image

NoReturn explanation:

Functions that raise exceptions and never return:

from typing import NoReturn

def fatal_error(message: str) -> NoReturn:
    """This function never returns; it always raises an exception."""
    raise RuntimeError(message)

fatal_error("Something went wrong!")  # 🚨 Raises RuntimeError

Here, fatal_error() always raises an exception and does not return any value.
Infinite loops (never terminating functions):

from typing import NoReturn

def infinite_loop() -> NoReturn:
    """This function never returns because it runs forever."""
    while True:
        print("Running forever...")

infinite_loop()  # 🔄 Runs indefinitely

Since the function runs indefinitely, it never reaches a return statement.
What NoReturn Does Not Mean
It does not mean the function returns None.
If a function returns nothing, it should be annotated as -> None instead.

def do_nothing() -> None:
    pass  # ✅ This function returns None, so NoReturn is NOT correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions