[Python-ideas] Introduce BaseTimeoutError

Chris Angelico rosuav at gmail.com
Sun Apr 2 00:38:41 EDT 2017


On Sun, Apr 2, 2017 at 1:11 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On the face of it, this isn't a serious problem. It seems to me rather
> like mistakenly writing:
>
>     except TypeError
>
> when you actually intended to write:
>
>     except ZeroDivisionError
>
> You failed to catch the exception that you needed, an got an exception.
> That's a bug, and the fix for it is to catch the correct exception. It
> isn't to introduce a new, more complex exception TypeOrZeroDivisionError
> that potentially catches too much.

What I'd like to see is a linter/static analyzer that can look at your
code and determine heuristically that it is highly unlikely for this
exception to be raised. With some exceptions, the assumption is "can
be raised anywhere", but with custom or library-specific exceptions,
only a raise statement can cause them. If you had a linter like that,
you could spot the wrong timeout being caught, as it would notify you
that there's no (normal) way for that to catch anything.

Note that I am adamantly NOT suggesting this as a language feature. A
linter is allowed to be wrong occasionally if it's helpful the rest of
the time, and it's allowed to ignore the possibilities of weird
aliasing or monkey-patching.

Does this already exist somewhere? Is there something people would recommend?

ChrisA


More information about the Python-ideas mailing list