[Python-ideas] Repurpose `assert' into a general-purpose check
p.f.moore at gmail.com
Tue Jan 16 13:35:17 EST 2018
On 16 January 2018 at 17:36, Sylvain MARIE
<sylvain.marie at schneider-electric.com> wrote:
> (trying with direct reply this time)
>> Why do you do this? What's the requirement for delaying evaluation of the condition?
> Thanks for challenging my poorly chosen examples :)
> The primary requirement is about *catching* unwanted/uncontrolled/heterogenous exceptions happening in the underlying functions that are combined together to provide the validation means, so as to provide a uniform/consistent outcome however diverse the underlying functions are (they can return booleans or raise exceptions, or both).
> In your proposal, if 'is_foo_compliant' raises an exception, it will not be caught by 'assert_valid', therefore the ValidationError will not be raised. So this is not what I want as an application developer.
Ah, OK. But nothing in your proposal for a new statement suggests you
wanted that, and assert doesn't work like that, so I hadn't realised
that's what you were after.
You could of course simply do:
def assert_valid(expr, help_msg):
# Catch exceptions in expr() as you see fit
if not expr():
assert_valid(lambda: 0 <= surf < 10000 and is_foo_compliant(surf),
help_msg="surface should be 0=<x<10000 or foo compliant")
No need for a whole expression language :-)
More information about the Python-ideas