except: without a type also catches KeyboardInterrupt and SystemExit
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.
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) 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:
[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.