flake8-bugbear
flake8-bugbear copied to clipboard
Report on uncatchable exception handlers (?)
Was surprised bugbear didn't report this, but not encountered in live code so I don't know if it's seen much / ever in the wild:
try:
...
except BaseException:
...
except:
...
will report B001/E722, but in a project that uses bare excepts widely and disables those checks the except never being possible to run seems like a separate error.
try:
...
except BaseException:
...
except Exception:
...
Gives no warning, nor does a plausibly more common
try:
...
except Exception:
...
except ValueError:
...
or similar variants. Checking whether a caught exception is a built-in, and if so issubclass of a previously caught exception, seems straightforward. I quickly checked mypy and vulture as well and neither of them sees it. But this might definitely not be common enough that it's worth bothering with.
For what it's worth, Pyright/Pylance catches this:
For what it's worth, Pyright/Pylance catches this:
It does not catch bare except after a BaseException (and since py3 exceptions have to inherit from baseexception), but indeed looks like it catches the other ones.
If nothing catches Exception (and children) being caught/except after BaseException, I think this is worth a check. BaseException catching in general is even more evil than Exception and I generally won't approve PRs that do it ...
I also like it checks all other exceptions too, as not everyone runs VSCode / IDE with this detection.