<div dir="ltr"><div>assert statement gives the possibility to display the text which goes along with the AssertionError exception. Most of the times though, what would be more appropriate is to raise a different exception (e.g. ValueError). My proposal is to be able to specify an exception as a replacement for AssertionError as in:</div><div><br>>>> assert callable(fun), ValueError("object is not a callable")</div><div>ValueError: object is not a callable<br></div><div><br></div><div>Specifically, this would be useful at the top of a function or method, where argument types or values are usually checked:<br><br><div>def retry(times=3, timeout=0.1, callback=None):</div><div>    assert times >= 1, ValueError("times must be >= 1")</div><div>    assert isinstance(timeout, (int, float)), ValueError("invalid timeout")</div><div>    assert callable(callback), ValueError("callback is not a callable")</div></div><div><br></div><div>...as opposed to:</div><div><br></div><div><div>def retry(times=3, timeout=0.1, callback=None):</div><div>    if not times >= 1:</div><div>        raise ValueError("times must be >= 1")</div><div>    if not isinstance(timeout, (int, float)):</div><div>        raise ValueError("invalid timeout")</div><div>    if not callable(callback):</div><div>        raise ValueError("callback is not a callable")</div></div><div><br></div><div>Other than saving 1 line for each type/value check, this has the advantage that the assertion logic (e.g. "times >= 1") is shown in the traceback message itself, because it's on the same line, enriching the context and giving more information in case of error.</div><div><br></div><div>Thoughts?</div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</div>