flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Report on uncatchable exception handlers (?)

Open jakkdl opened this issue 2 years ago • 3 comments
trafficstars

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.

jakkdl avatar Feb 07 '23 10:02 jakkdl

For what it's worth, Pyright/Pylance catches this:

Screen Shot 2023-02-07 at 2 48 55 AM

JelleZijlstra avatar Feb 07 '23 10:02 JelleZijlstra

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.

jakkdl avatar Feb 07 '23 10:02 jakkdl

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.

cooperlees avatar Feb 08 '23 01:02 cooperlees