Skip to content

Add rule for improper use of NoReturn  #9113

@SRv6d

Description

@SRv6d

The NoReturn return type is commonly misunderstood and not very intuitive.

It should be used for functions that never return, either through unconditionally raising an exception,
or by exiting.

Yet, I commonly see it misused (and have misused it myself) to annotate functions that may raise or return using a union return value, eg:

def raise_conditionally(raise_exc: bool) -> None | NoReturn:
    if raise_exc:
        raise RuntimeError("raise_exc was true")
    return None

Although ideally ruff would ensure the full invariant(no use of NoReturn for functions that might do anything other than raise or exit), I haven't looked into how much work it would be to implement that and simply ensuring no use of union types with NoReturn would go a long way.

Metadata

Metadata

Assignees

Labels

acceptedReady for implementationruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions