[Python-Dev] the role of assert in the standard library ?

Tarek Ziadé ziade.tarek at gmail.com
Thu Apr 28 14:09:17 CEST 2011


On Thu, Apr 28, 2011 at 12:27 PM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> On 28/04/2011 09:34, Terry Reedy wrote:
>>
>> On 4/28/2011 3:54 AM, Tarek Ziadé wrote:
>>>
>>> Hello
>>>
>>> I removed some assert calls in distutils some time ago because the
>>> package was not behaving correctly when people were using Python with
>>> the --optimize flag. In other words, assert became a full part of the
>>> code logic and removing them via -O was changing the behavior.
>>>
>>> In my opinion assert should be avoided completely anywhere else than
>>> in the tests. If this is a wrong statement, please let me know why :)
>>
>> My understanding is that assert can be used in production code but only to
>> catch logic errors by testing supposed invariants or postconditions. It
>> should not be used to test usage errors, including preconditions. In other
>> words, assert presence or absence should not affect behavior unless the code
>> has a bug.
>
> Agreed. We should ideally have buildbots doing test runs with -O and -OO. R.
> David Murray did a lot of work a year ago (or so) to ensure the test run
> passes with -OO but it easily degrades..
>
> There are a couple of asserts in unittest (for test discovery) but I only
> use them to provide failure messages early. The functionality is unchanged
> (and tests still pass) with -OO.
>
> All the best,

I'll try to add a useful report on "bad asserts" in the bug tracker.

I am replying again to this on Python-ideas because I want to debate
on assert :)

Cheers
Tarek


More information about the Python-Dev mailing list