Description
Describe the bug
Given this program as test.py
:
def func():
val = next((c for c in "abc"), None)
print(val)
func()
Branch coverage claims that there is an uncovered arc from line 2 (the val = next(...)
) to exit
. As far as I can see, there is no branch there.
To Reproduce
$ python -V
Python 3.11.2
$ coverage --version
Coverage.py, version 7.2.4 with C extension
Full documentation is at https://coverage.readthedocs.io/en/7.2.4
$ coverage run --branch test.py
a
$ coverage report --show-missing
Name Stmts Miss Branch BrPart Cover Missing
-----------------------------------------------------
test.py 4 0 2 1 83% 2->exit
-----------------------------------------------------
TOTAL 4 0 2 1 83%
Interestingly, if you change line 2 to:
val = next(iter("abc"), None)
Coverage no longer believes there is an uncovered branch, despite the fact that (c for c in "abc")
and iter("abc")
both return iterators over the same 3 elements.
Expected behavior
There should not be a branch detected from line 2 to exit, as the only line that can follow 2 is 3. I suspect the issue may perhaps be that Coverage doesn't understand that 2-argument next
will never raise a StopIteration
.
Additional context
This may be the same issue as #605 (comment) - I started to add this as a comment there, but then thought I'd err on the side of a new issue.