This PEP proposes disallowing bare except: clauses in Python’s exception-handling syntax. Currently, Python allows catching all exceptions with a bare except: clause, which can lead to overly broad exception handling and mask important errors. This PEP suggests requiring explicit exception types in all except clauses, promoting more precise and intentional error handling.
Please review the entire document before commenting as it contains several aspects of how to address the deprecation, tooling, rejected ideas and more.
I know this is off-topic for this thread, but I raised this as an issue with the PEP 760 PR (the PEP itself hasn’t been published yet). I haven’t had time to look at the response from the PEP authors, but I would really like to preserve bare-except with an unconditional raise use case. If that can’t be done, I suggested an alias for BaseException called AnyException so at least the modified code would read better:
Can I ask why? I don’t find that any clearer than except BaseException, and if you are using that pattern I feel you should understand what you’re doing. As such, I don’t think learning what BaseException means is too much to ask.
It’s the comma in the appropriate contex; not every comma makes a tuple, but you can’t syntactically make a tuple without a comma.
BaseException exposes the fact that it’s the root class in the exception hierarchy, and you have to connect that mentally to the fact that a) Python requires all exceptions to derive from it, and b) that pattern means “catch any exception”. The latter just makes that obvious. It’s obvious to us of course either way.
For me, I’d still prefer to allow a bare except if there’s an explicit empty raise in the body.
I don’t think that handles the original use case: abort an in-progress transaction and re-raise when any exception occurs, and commit the transaction if no exception occurs.
+1 to keeping bare except: with raise.
For better or worse, it’s correct, idiomatic code; the “Motivation” reasons don’t really apply to it. pylint and ruff allow this, and the the style guides their docs link to (PEP-8 and the Google style guide) also mention that it’s OK. (flake8 does flag it, though, and some of the other guides don’t list the exception.)
And unlike finally e: or implicit raise, the existing syntax is compatible with Python 1.0+.
Although it’s straightforward the implementation would be quite verbose as this implies doing an entire ast visitor just to detect raises below the bare except (in C).
The task becomes much simpler if we restrict it to detecting raises only at the same level of nesting but that feels somewhat odd.
I think this is an interesting case but you could skip all of this by just doing except BaseException and then all is good.
This breaks so much existing code. Does the benefit outweigh this? IMHO it’s OK to emit warnings left and right, but outright rejecting bare excepts after just 3 releases of deprecation period seems… not cool. Please, be more conservative with breaking backward compatibility of the basic language. This is not an API of one standard library module, this is the Python language itself.
I think requiring this is reasonable but I am uncomfortable with the maintenance consequences of having to keep an entire visitor just for restricting this too much. The exception also feels slightly odd to communicate when really we can avoid all of this by just writing except BaseException