A value that is not an exception is raised, caught or given as a cause

tx0/python:not-an-exceptiondefaultwarningbug

What it finds

raise "failed", except int:: only classes deriving from BaseException, and their instances, can be raised, and only such classes can be caught. Raising anything else raises TypeError instead, and an except clause naming something else raises TypeError as soon as an exception reaches it. The cause after raise ... from must be an exception or None.

Reported are literals, displays, and builtins that are not exceptions, such as str or int and calls of them, in those places. tuple(...) in an except clause is a tuple of classes, which is fine.

From the rule's source, not_an_exception.rs.

Example from the rule's tests
def check(value, error):    if value is None:        raise "missing value"tx0 reports: is not an exception, so raising it raises TypeError    if value < 0:        raise ValueError("negative") from "input"tx0 reports: is not an exception or None, so it cannot be a cause    if value > 9:        raise error    try:        return 1 / value    except int:tx0 reports: int is not an exception class, so this clause raises TypeError        return 0    except (ZeroDivisionError, str()):tx0 reports: str() is not an exception class        return 0    except ValueError as exc:        raise RuntimeError("bad") from None
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:not-an-exception" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:not-an-exception] reason="..."

tx0 explain tx0/python:not-an-exception shows the same in the terminal. Found a false positive? Report it.