except: without a type also catches KeyboardInterrupt and SystemExit

tx0/python:bare-exceptrecommendedwarningbug

What it finds

except: catches every exception, including KeyboardInterrupt and SystemExit, which stop the program on Ctrl-C and on sys.exit(), and GeneratorExit. A handler that ends with a bare raise lets them pass on and is not reported, and neither is one after a clause that handles SystemExit, KeyboardInterrupt or BaseException.

This rule is in the recommended tier: catching everything is right in some places, and except BaseException: then says so.

From the rule's source, bare_except.rs.

Example from the rule's tests
def load(path, log):    try:        return open(path).read()    except:tx0 reports: except: also catches KeyboardInterrupt and SystemExit        return None  def cleanup(path, log):    try:        return open(path).read()    except:        log("failed")        raise  def run(code, log):    try:        exec(code)    except SystemExit:        raise    except:        log("error")  def typed(path, log):    try:        return open(path).read()    except OSError:        return None    except Exception as error:        log(error)
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-except" = "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-except] reason="..."

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