A finally block jumps away with return, break or continue and drops any pending exception

tx0/python:finally-control-flowdefaultwarningbug

What it finds

return, break or continue in a finally block: leaving the block this way discards the exception that may be on its way out of the try, and replaces the value a return in the try was returning. PEP 765 made these statements warnings in Python 3.14. Statements in functions or classes defined inside the block, and break and continue of loops inside it, do not leave it and are not reported.

From the rule's source, finally_control_flow.rs.

Example from the rule's tests
def first(items, log):    try:        return items[0]    finally:        return Nonetx0 reports: return in a finally block discards any exception in flight  def loop(items, log):    for item in items:        try:            log(item)        finally:            if item:                breaktx0 reports: break in a finally block            continuetx0 reports: continue in a finally block  def fine(items, log):    try:        log(items)    finally:        for item in items:            if item:                break            continue        while items:            break        else:            log(items)         def callback():            return 1         log(callback)    return items  def nested(items, log):    for item in items:        try:            log(item)        finally:            for other in items:                log(other)            else:                breaktx0 reports: break in a finally block            try:                log(item)            finally:                return itemtx0 reports: return in a finally block
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:finally-control-flow" = "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:finally-control-flow] reason="..."

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