New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A better assert statement #70889
Comments
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. |
I think in this particular case you are more interesting in the value of k than k.replace('.', '').replace('-', '').replace('_', ''). |
On Apr 06, 2016, at 03:07 PM, Serhiy Storchaka wrote:
Possibly so. |
I'm -1 for changing syntax of assert. But I think that it may be useful to change semantic of assert so that all intermediate results of subexpressions are saved and included in the assert report (or at least their shortened reprs). I'm +0 for this. |
Maybe we could use the walrus operator for this: make assert display the reprs of all named sub-expressions. |
Just a note and possible design inspiration, pytest has pretty assertions like this to allowing you to write unit tests purely with the assert statement and not unittest's special |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: