[Python-ideas] Specify an alternative exception for "assert"

Franklin? Lee leewangzhong+python at gmail.com
Tue May 3 11:36:34 EDT 2016


On May 2, 2016 11:18 AM, "Random832" <random832 at fastmail.com> wrote:
>
> On Mon, May 2, 2016, at 11:14, Guido van Rossum wrote:
> > On Mon, May 2, 2016 at 8:01 AM, Ryan Gonzalez <rymg19 at gmail.com> wrote:
> >
> > > Other than the fact that this would completely fail when run with
-O...
> > >
> > But maybe that's fine, or intended.
> >
> > I can see a fair number of uses for this, including subclasses of
> > AssertionError.
>
> I think this would be an attractive nuisance, and if it's implemented at
> all it should forbid types that are *not* subclasses of AssertionError
> in order to mitigate that.

What about wrapping the error in an AssertionError? The error being raised
is really an assertion error, since it came from an assert.

A wrapped ValueError isn't going to be caught by an `except ValueError:`
clause. That might be a good thing: failed asserts usually shouldn't be
caught, since they indicate a bug. Even if they are caught (e.g. for
logging), they shouldn't be caught by something expecting a
non-AssertionError.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160503/28f2081a/attachment.html>


More information about the Python-ideas mailing list