A bare raise stands in a finally block, which also runs without an exception

tx0/python:bare-raise-in-finallyrecommendedwarningbug

What it finds

A bare raise in a finally block: it re-raises the exception being handled, but a finally block also runs when no exception was raised, and then the raise itself fails with RuntimeError: No active exception to re-raise, which hides what happened.

A raise in a finally block that is itself inside an except clause, or that has an except clause around it in the finally block, re-raises what that clause handles, and is left alone.

From the rule's source, bare_raise_in_finally.rs.

Example from the rule's tests
def close(handle):    try:        handle.flush()    finally:        handle.close()        raisetx0 reports: this raise fails with RuntimeError when the finally block runs without an exception  def cleanup(handle):    try:        handle.flush()    finally:        try:            handle.close()        except OSError:            raise
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:bare-raise-in-finally" = "warning"

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:bare-raise-in-finally] reason="..."

tx0 explain tx0/python:bare-raise-in-finally shows the same in the terminal. Found a false positive? Report it.