Unroll the iterable for all/any calls to get better reports
pytest-dev/pytest · SWE-bench Lite · Reference fix
Instruction
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!
For example - the same test written in three ways:
- A generator expression
def test_all_even():
even_stevens = list(range(1,100,2))
> assert all(is_even(number) for number in even_stevens)
E assert False
E + where False = all(<generator object test_all_even.<locals>.<genexpr> at 0x101f82ed0>)
- A list comprehension
def test_all_even():
even_stevens = list(range(1,100,2))
> assert all([is_even(number) for number in even_stevens])
E assert False
E + where False = all([False, False, False, False, False, False, ...])
- A for loop
def test_all_even():
even_stevens = list(range(1,100,2))
for number in even_stevens:
> assert is_even(number)
E assert False
E + where False = is_even(1)
test_all_any.py:7: AssertionError
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)
I propose the assertion re-writer "unrolls" the iterator to the third form, and then uses the already existing reports.
- Include a detailed description of the bug or suggestion
-
pip listof the virtual environment you are using
Package Version
-------------- -------
atomicwrites 1.3.0
attrs 19.1.0
more-itertools 7.0.0
pip 19.0.3
pluggy 0.9.0
py 1.8.0
pytest 4.4.0
setuptools 40.8.0
six 1.12.0
- pytest and operating system versions
platform darwin -- Python 3.7.3, pytest-4.4.0, py-1.8.0, pluggy-0.9.0 - Minimal example if possible
pytest-dev__pytest-5103 · SWE-bench Lite problem statement, verbatim
| Search tool | Functions found |
|---|---|
| noodlbox | 2 of 2 |
| ripgrep with targeted reads | 0 of 2 |
| 2.3.18 harness-scored | |
Token positions count from the start of noodlbox’s returned text. File excerpts are numbered in the order ripgrep with targeted reads returned them.
| Function | noodlboxFirst appears in returned text | ripgrep with targeted readsFirst matching file excerpt |
|---|---|---|
src/_pytest/assertion/rewrite.py | 2.3.18 harness-scored | |
visit_Call_legacy | At token 756 | 12 of 341 |
visit_Call_35 | At token 858 | 12 of 341 |
| Measurement | noodlbox | ripgrep with targeted readsNo output cap |
|---|---|---|
| To find all 2 functions | 858 | 27,461 |
| Total returned | 15,334 | 363,685 |
| 2.3.18 harness-scored | ||