This proposal sounds very promising. Some observations/questions follow.
On Fri, Sep 24, 2021 at 02:04:21PM +0300, Noam Tenne wrote:
> Assertion failed:
>
> sc.val['d'] == f
> | | |
> | e False
> |
> {'d': 'e'}
Can we see what the output would look like in larger, more complex
examples? Especially those that cross line boundaries?
strict = False
direction = 'left'
sym = 'N'
count = 2
clusters = [None]
assert ((strict and direction is not None)
or (sym == 'N' and count == 1 and len(clusters) == 1)
), "description ..."
How about examples where one of the values is extremely large?
sc.val = {'d': 'e'}
sc.val.update(dict.fromkeys(range(100))
assert sc.val['d'] == 'f'
If the handler is a hook in sys, like displayhook and except hook, then
people can install their own handlers. But I think the default handler
(asserthook?) should do the right thing for ugly assertions.
> In the python-ideas mailing list, Serhiy Storchaka suggests:
>
> It needs a support in the compiler. The condition expression should be
> compiled to keep all immediate results of subexpressions on the stack.
> If the final result is true, immediate results are dropped. If it is
> false, the second argument of assert is evaluated and its value together
> with all immediate results of the first expression, together with
> references to corresponding subexpressions (as strings, ranges or AST
> nodes) are passed to the special handler. That handler can be
> implemented in a third-party library, because formatting and outputting
> a report is a complex task. The default handler can just raise an
> AttributeError.
Is that supposed to be AssertionError?
> It is important to note that expressions with side effects are
> affected by this feature. This is because in order to display this
> information, we must store references to the instances and not just
> the values.
Don't you mean that expressions with side-effects are *not* affected? Or
have I misunderstood?
You don't have to re-evaluate the expression if the assertion fails, so
any side-effects only occur once.
greeting = "Hello world"
assert print(greeting)
Hello world
Assertion failed:
print(greeting)
| |
| 'Hello world'
|
None
Rather than the situation where the expression has to be evaluated
twice (as older versions of Pytest used to do):
greeting = "Hello world"
assert print(greeting)
Hello world
Assertion failed:
print(greeting)
Hello world
| |
| 'Hello world'
|
None
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TMD6RNVZPFTMYP3F2D3ECUAAI5EURTMD/
Code of Conduct: http://python.org/psf/codeofconduct/