New submission from Nick Coghlan <ncogh...@gmail.com>:

This proposal is an outcome of repeated requests on python-ideas that assert 
statements be made unconditional, so they can be legitimately used for 
parameter validation, rather than solely as a form of inline self-test.

Rather than changing the assert statement, an alternative option would be to 
provide a new builtin (suggested name: "ensure") that raises ValidationError (a 
new subclass of AssertionError) if the first argument is false.

As a function, the new builtin could accept parameters for:

- the message to be reported on failure
- the exception type to be raised on failure

And since it would only be a new builtin rather than a new keyword, existing 
uses of the name "ensure" would be unaffected (except to the extent that 
linters may start warning about shadowing a builtin).

(Since it's a suggestion for a new builtin, actually doing this would require a 
PEP, which I'm not planning to write, I just wanted to get the suggestion 
explicitly on the record rather than leaving it buried in mailing list archives)

----------
messages: 310219
nosy: ncoghlan
priority: normal
severity: normal
stage: needs patch
status: open
title: Proposal: add an "ensure(arg)" builtin for parameter validation
type: enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32590>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to