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

Giampaolo Rodola' g.rodola at gmail.com
Mon May 2 13:45:15 EDT 2016

On Mon, May 2, 2016 at 6:32 PM, Ryan Gonzalez <rymg19 at gmail.com> wrote:

> On May 2, 2016 10:55 AM, "Guido van Rossum" <guido at python.org> wrote:
> >
> > On Mon, May 2, 2016 at 8:38 AM, Ryan Gonzalez <rymg19 at gmail.com> wrote:
> >>
> >> I feel like you just brought up the exact issue. Assertions should
> *never* be used for things like this. Because, one day, some stupid idiot
> is going to use assertions to perform some sort of data validation, someone
> else will use that library with -O, and the world will explode.
> >
> > That's a very strong opinion, and a bit of a doomsday scenario. I hear
> worries about this a lot, but I've never heard a story from someone to whom
> this actually happened.
> >>
> >> It's just far too attractive to use but far too easy to misuse.
> >
> > The thing is, assert exists and is not going away. People are writing
> these asserts today (there are still many in asyncio). In many cases the
> assert is not guarding against bad data or user input, but simply against a
> confused caller. The assumption is that once the program is debugged enough
> to go to production (where you use -O) the need for the asserts has gone
> down enough that it doesn't matter the checks are gone -- the confused call
> site has long been found during tests and fixed.
> I would still prefer the proposal that requires the errors to derive from
> AssertionError
The proposal I'm making is the exact opposite (allow a different exception
than AssertionError) so I'm not sure I understand your objection.

> , since there *are* people that will use this for some sort of data
> validation.
Personally I never used asserts for data validation. Usually they are used:

#1 in unit tests, if you don't like using the verbose unittest's
self.assert* APIs
#2 in application code as an extra safety to make sure, e.g., that the
function's caller is passing the "right things", that an attribute has a
certain value at some point, etc.

Note that #2 is meant to help the developer(s), not the final user. It's
something which is there as a guard against possible mistakes but not much
more, and to my understanding this is how most people feel about
"assert"s.  Could allowing to raise a specific and "more correct" exception
type as a one-liner be an incentive to use "assert" to validate user input?
I don't know, but my impression is that:

#1 most people are "educated" enough to know that assert is usually not
meant to validate user input
#2 very few people use -O (e.g. I believe I never used it myself)
#3 I expect whoever uses -O is supposed to be aware of what are the
implications of it

Giampaolo - http://grodola.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160502/dd518fd6/attachment-0001.html>

More information about the Python-ideas mailing list