Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3.8 try-finally-return reports wrong branch coverage with async functions #964

Closed
afflux opened this issue Mar 24, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@afflux
Copy link
Contributor

afflux commented Mar 24, 2020

Describe the bug
I'm afraid I have a similar case to #707, although somehow, decorators and coroutine functions are involved.

To Reproduce

  1. What version of Python are you using? python3.8
  2. What version of coverage.py are you using? 5.0.4., sys.txt attached
  3. What versions of what packages do you have installed? pip-freeze.txt
  4. What commands did you run? coverage run --branch test.py ; coverage html
  5. What code are you running?
import asyncio

def foo(x):
    return x

@foo
async def f1():
    try:
        return 2
    finally:
        print('finally')

asyncio.run(f1())

Expected behavior
Full branch coverage for f1()

Additional context
systrace has frame.f_code.co_firstlineno on the line with the @foo decorator.
When f1 is async, AstArcAnalyzer finds an arc to the line containing async def:

Adding arc: (11, -9): "the return on line {lineno} wasn't executed", "didn't return from function 'f1'"
     _code_object__FunctionDef : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:1155
                 add_body_arcs : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:690
                      add_arcs : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:653
                  _handle__Try : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:1059
          process_return_exits : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:843
                       add_arc : /home/kb/repos/pysomeip/.tox/coverage/lib/python3.8/site-packages/coverage/parser.py:563

If I make f1 non-async, AstArcAnalyzer finds the line with the decorator:

Adding arc: (11, -8): "the return on line {lineno} wasn't executed", "didn't return from function 'f1'"
[same stack]
@afflux afflux added the bug Something isn't working label Mar 24, 2020
afflux added a commit to afflux/coveragepy that referenced this issue Mar 24, 2020
@nedbat nedbat closed this as completed in 0db15a2 Mar 26, 2020
nedbat added a commit that referenced this issue Mar 26, 2020
handle decorators for AsyncFunctionDefs. Closes #964
@nedbat
Copy link
Owner

nedbat commented Apr 12, 2020

This is available in 5.1: https://pypi.org/project/coverage/5.1/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants