Repository navigation
Description
Describe the bug
Coverage 7.6.3 appears to have introduced a regression in the calculation of branch coverage related to the handling of while loops inside context managers.
To Reproduce
How can we reproduce the problem? Please be specific. Don't link to a failing CI job. Answer the questions below:
- What version of Python are you using?
Observed using 3.12.7 and 3.13.0. It doesn't appear in 3.11 or earlier.
- What version of coverage.py shows the problem? The output of
coverage debug sysis helpful.
7.6.3. The problem doesn't exist in 7.6.1.
- What versions of what packages do you have installed? The output of
pip freezeis helpful. - What code shows the problem? Give us a specific commit of a specific repo that we can check out. If you've already worked around the problem, please provide a commit before that fix.
The problem has been found in this dependabot update on Briefcase, which bumps coverage.py from 7.6.1 to 7.6.3.
- What commands should we run to reproduce the problem? Be specific. Include everything, even
git clone,pip install, and so on. Explain like we're five!
$ git clone [email protected]:beeware/briefcase.git
$ cd briefcase
$ git checkout -b coverage-7.6.3 origin/dependabot/pip/coverage-toml--7.6.3
$ python3.12 -m venv venv
$ source venv/bin/activate
(venv) $ pip install tox
$ tox -m test312
This will run the full test suite, reporting 2 branch coverage misses:
...
coverage312: commands[1]> python -m coverage report --fail-under=100
Name Stmts Miss Branch BrPart Cover Missing
----------------------------------------------------------------------------------
src/briefcase/platforms/iOS/xcode.py 219 0 48 2 99.3% 525->528, 541->544
----------------------------------------------------------------------------------
TOTAL 7084 0 1598 2 99.9%
67 files skipped due to complete coverage.
Expected behavior
If you omit the git checkout -b ... line, the main branch uses 7.6.1, and shows 100% coverage.
...
coverage312: commands[1]> python -m coverage report --fail-under=100
Name Stmts Miss Branch BrPart Cover Missing
----------------------------------------------------
TOTAL 7104 0 2354 0 100.0%
68 files skipped due to complete coverage.
Additional context
It seems likely this is related to #1876, and commit 378c321.
Both of the new "missing" branches follow the same basic pattern of a while loop with a walrus operator, inside a context manager:
Unfortunately, I haven't had any luck reducing this to a simpler example case.
Activity
nedbat commentedon Oct 19, 2024
Thanks, I can reproduce it. It seems to have been introduced with 7.6.2.
udifuchs commentedon Oct 19, 2024
I have a very similar issue. False positive in a for loop inside a context manager. coverage 7.6.1 worked correctly. 7.6.2 and 7.6.3 fail.
The difference is that it shows up with python 3.13, but not with python 3.12.
The bug shows up in
pylint-silentbranchcoverage-branching:And can be reproduced using:
nedbat commentedon Oct 20, 2024
@udifuchs Thanks, but I'm not sure I have enough information to reproduce. When I clone your repo and checkout that branch, I get two test failures:
Full output
nedbat commentedon Oct 20, 2024
The walrus was a different animal: a red herring. The key here was the grouped
withsyntax:Running this produces this report:
Even having just one context manager, the parenthesized syntax causes the problem... Now to debug.
fix: multi-line with-statements exit correctly. #1880
fix: multi-line with-statements exit correctly. #1880
nedbat commentedon Oct 20, 2024
Fixed in commit b8c236a.
freakboy3742 commentedon Oct 20, 2024
Bah! What have parentheses done for us lately? :-P
Thanks for the fix, @nedbat.
nedbat commentedon Oct 20, 2024
This is now released as part of coverage 7.6.4.
udifuchs commentedon Oct 21, 2024
I noticed that you are using macos. Apparently, my tests did not work on macos. Here is a branch that fixes that:
https://github.com/udifuchs/pylint-silent/tree/refs/heads/macos-threads
I don't have access to macos, but here is a successful run with github actions:
https://github.com/udifuchs/pylint-silent/actions/runs/11432810880
But the real issue is that this bug still shows up with python 3.12.3. The tests in coverage.py fail when run with python 3.12.3.
nedbat commentedon Oct 21, 2024
@udifuchs is your problem fixed by coverage 7.6.4?
udifuchs commentedon Oct 21, 2024
No. With coverage 7.6.4 python 3.12.3 still has the same bug. It is reproduced here:
https://github.com/udifuchs/pylint-silent/actions/runs/11432842782
It can also be reproduced by running coveragepy test with python 3.12.3.
nedbat commentedon Oct 21, 2024
I see what you mean. 3.12.3 shows that problem. So do 3.12.4 and 3.12.5. But 3.12.6 and 3.12.7 do not. Can you upgrade your Python?
fix: with-exiting changed in 3.12.6. #1880
nedbat commentedon Oct 22, 2024
I updated coverage.py to decide between 3.12.5 and 3.12.6, so it will work properly with 3.12.3 now. But it hasn't been released yet. Install from git to try it out.
udifuchs commentedon Oct 22, 2024
I installed from git coveragepy version 7.6.5a0.dev1 and it works correctly with python 3.12.3.
The reason I am using python 3.12.3 is that this is the packaged version in Ubuntu 24.04.