On 18 January 2018 at 07:46, Steven D'Aprano <st...@pearwood.info> wrote:
> To justify a keyword, it needs to do something special that a built-in
> function can't do, like delayed evaluation (without wrapping the
> expression in a function).

My reaction to these threads for a while has been "We should just add
a function for unconditional assertions in expression form", and I
finally got around to posting that to the issue tracker rather than
leaving it solely in mailing list posts:
https://bugs.python.org/issue32590

The gist of the idea is to add a new ensure() builtin along the lines of:

    class ValidationError(AssertionError):
        pass

    _MISSING = object()

    def ensure(condition, msg=_MISSING, exc_type=ValidationError):
        if not condition:
            if msg is _MISSING:
                msg = condition
            raise exc_type(msg)

There's no need to involve the compiler if you're never going to
optimise the code out, and code-rewriters like the one in pytest can
be taught to recognise "ensure(condition)" as being comparable to an
assert statement.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to