Elffuss BraidHub

kikocis/auto-code-rover public

main · 2.2 KiBRawPermalink
1Unroll the iterable for all/any calls to get better reports
2Sometime I need to assert some predicate on all of an iterable, and for that the builtin functions `all`/`any` are great - but the failure messages aren't useful at all!
3For example - the same test written in three ways:
4
5- A generator expression
6```sh
7 def test_all_even():
8 even_stevens = list(range(1,100,2))
9> assert all(is_even(number) for number in even_stevens)
10E assert False
11E + where False = all(<generator object test_all_even.<locals>.<genexpr> at 0x101f82ed0>)
12```
13- A list comprehension
14```sh
15 def test_all_even():
16 even_stevens = list(range(1,100,2))
17> assert all([is_even(number) for number in even_stevens])
18E assert False
19E + where False = all([False, False, False, False, False, False, ...])
20```
21- A for loop
22```sh
23 def test_all_even():
24 even_stevens = list(range(1,100,2))
25 for number in even_stevens:
26> assert is_even(number)
27E assert False
28E + where False = is_even(1)
29
30test_all_any.py:7: AssertionError
31```
32The only one that gives a meaningful report is the for loop - but it's way more wordy, and `all` asserts don't translate to a for loop nicely (I'll have to write a `break` or a helper function - yuck)
33I propose the assertion re-writer "unrolls" the iterator to the third form, and then uses the already existing reports.
34
35- [x] Include a detailed description of the bug or suggestion
36- [x] `pip list` of the virtual environment you are using
37```
38Package Version
39-------------- -------
40atomicwrites 1.3.0
41attrs 19.1.0
42more-itertools 7.0.0
43pip 19.0.3
44pluggy 0.9.0
45py 1.8.0
46pytest 4.4.0
47setuptools 40.8.0
48six 1.12.0
49```
50- [x] pytest and operating system versions
51`platform darwin -- Python 3.7.3, pytest-4.4.0, py-1.8.0, pluggy-0.9.0`
52- [x] Minimal example if possible
53