| 1 | Unroll the iterable for all/any calls to get better reports |
| 2 | Sometime 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! |
| 3 | For 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) |
| 10 | E assert False |
| 11 | E + 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]) |
| 18 | E assert False |
| 19 | E + 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) |
| 27 | E assert False |
| 28 | E + where False = is_even(1) |
| 29 | |
| 30 | test_all_any.py:7: AssertionError |
| 31 | ``` |
| 32 | The 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) |
| 33 | I 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 | ``` |
| 38 | Package Version |
| 39 | -------------- ------- |
| 40 | atomicwrites 1.3.0 |
| 41 | attrs 19.1.0 |
| 42 | more-itertools 7.0.0 |
| 43 | pip 19.0.3 |
| 44 | pluggy 0.9.0 |
| 45 | py 1.8.0 |
| 46 | pytest 4.4.0 |
| 47 | setuptools 40.8.0 |
| 48 | six 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 | |