New submission from Barry A. Warsaw:

Too many times I hit failing assert statements, and have no idea what value is 
causing the assertion to fail.  Sure, you can provide a value to print (instead 
of just the failing code) but it seems to be fairly rarely used.  And it can 
also lead to code duplication.  As an example, I saw this today in some 
installed code:

assert k.replace('.', '').replace('-', '').replace('_', '').isalum()

So now I have to sudo edit the installed system file, duplicate everything up 
to but not including the .isalum() as the second argument to assert, then try 
to reproduce the problem.  IWBNI assert could make this better.

One idea would be to split the value and the conditional being asserted on that 
value, but we can't use two-argument asserts for that.  Crazy syntax thought: 
reuse the 'with' keyword:

assert k.replace('.', '').replace('-', '').replace('_', '') with isalum

where the part before the 'with' is 'value' and the part after the 'with' is 
conditional, and the two parts together imply the expression.

This would be equivalent to:

if __debug__:
    if not value.conditional():
        raise AssertionError(expression, value, conditional)

I suppose you then want to support arguments:

assert foo with can_bar, 1, 2, x=3

but maybe that's YAGNI and we can just say to use a better 2-value assert in 
those more complicated cases.

----------
messages: 262946
nosy: barry
priority: normal
severity: normal
status: open
title: A better assert statement
versions: Python 3.6

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

Reply via email to