Why assert is not a function?

Barry Scott barry at barrys-emacs.org
Wed Mar 3 14:33:27 EST 2021



> On 3 Mar 2021, at 18:41, Chris Angelico <rosuav at gmail.com> wrote:
> 
> On Thu, Mar 4, 2021 at 5:25 AM Barry Scott <barry at barrys-emacs.org <mailto:barry at barrys-emacs.org>> wrote:
>> 
>> 
>> 
>>> On 3 Mar 2021, at 17:35, David Lowry-Duda <david at lowryduda.com> wrote:
>>> 
>>>> assert condition, expression
>>>> 
>>>> Only is condition is false with expression be evaluated.
>>>> So you can safely do expensive things I the expression with incuring
>>>> and cost if the condition is True.
>>> 
>>> I think I've only every used a string as the expression. Have you found
>>> it useful to use complicated, expensive expressions? Could you give an
>>> example?
>> 
>> In the test suite of the product I work on its common to put a lot of information into the expression.
>> For example if a HTTP request returns unexpected content we will include all the headers and
>> body of the that the request returners.
>> 
>>        assert http_status == '200', 'Request failed status %r Body:\n%s' % (http_status, http_header_and_body)
>> 
> 
> That doesn't look like the sort of thing that should be an assertion.
> Assertions should be for things that, if your code were bug-free,
> could never happen.

If the code works the status will be 200 and the test will pass.

Now you can argue that the test suite should use something better then assert,
but assert is what the code that I inherited uses. Its origins go back many many
years. Its would be a lot of work to modernise as there are 1,000s of test suites
affect.

Barry


> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>


More information about the Python-list mailing list