On 10.09.2021 05:49, Steven D'Aprano wrote:
> What I question is that allowing assert to raise non-assertions will 
> lead to *more* resilient software rather than less.
> 
> I know far too many people who insist on abusing assertions as a lazy 
> way to validate caller- or user-supplied data, which is **not** a 
> contract.

I concur.

asserts are meant for verifying assumptions the code designer
and wants to verify *during development*.

In C they lead to a core dump which aids in finding the cause
of the problem. In Python, an AssertionError is raised with the
same intent.

In Python, using assert for anything that is not development
related is dangerous, since production code running with
-O (optimized mode) will not even run those assertions -
for a good reason: they are meant only for checking assumptions
in development.

For any non-development related error checking, normal if
statements should be used, not assert.

I very often see code bases, which abuse assert as a quick
way to check types, ranges, valid flag combinations, etc.

Since all these things can go wrong in production as well,
those checks need to be handled with if statements, not
assert.

If we'd not allow assert to also raise non-AssertionErrors,
we'd get even more abuse.

In fact, I'd be in favor of deprecating assert altogether,
if it were not for pytest using it for testing - which is a
valid use case, since those tests are not run in production,
but again, most likely leads to programmers thinking that
they can use the same logic in the actual production code.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Sep 10 2021)
>>> Python Projects, Coaching and Support ...    https://www.egenix.com/
>>> Python Product Development ...        https://consulting.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               https://www.egenix.com/company/contact/
                     https://www.malemburg.com/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/466LNIX4FGGSTBFFC5PMLIQKQLPQCKJJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to