crates/ty_python_semantic/resources/lint_docs/invalid-raise.md
Checks for raise statements that raise non-exceptions or use invalid causes for their raised
exceptions.
Only subclasses or instances of BaseException can be raised. For an exception's cause, the same
rules apply, except that None is also permitted. Violating these rules results in a TypeError at
runtime.
def something():
raise NameError
def cause() -> None:
pass
def f():
try:
something()
except NameError:
# error: "Cannot raise object of type `Literal["oops!"]`"
# error: "Cannot use object of type `def cause() -> None` as an exception cause"
raise "oops!" from cause
def g():
# error: "Cannot raise `NotImplemented`"
# error: "Cannot use object of type `Literal[42]` as an exception cause"
raise NotImplemented from 42
Use instead:
def something():
raise NameError
def f():
try:
something()
except NameError as e:
raise RuntimeError("oops!") from e
def g():
raise NotImplementedError from None