python3.8 try-finally-return reports wrong branch coverage with async functions · Issue #964 · nedbat/coveragepy (original) (raw)

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]