Missed lines with asyncio & context managers

Issue #441 resolved
Hynek Schlawack created an issue

I have a rather odd problem I cannot reproduce in clean room.

I have a rather simple class and method:


Line 21 – or any line before the except – is not reported as covered. Even if it’s a print statement and see its output.

It seems to be related with the context manager because it's a) always the last line and b) when I rewrite it as https://gist.github.com/hynek/cfe2cc32f337e2676466 the coverage works just fine.

I’m sorry I can’t be more helpful but I’m confused as hell myself. The pool is from aiopg and seems to be semi-magical: https://github.com/aio-libs/aiopg/blob/master/aiopg/pool.py#L302 so dunno if that helps…

Comments (12)

  1. Ned Batchelder repo owner

    @hynek any chance you've come up with more information about this? Can you provide explicit instructions for reproducing it, even if it's a particular hash in your repo with details of how to run the tests?

  2. Hynek Schlawack reporter

    I was able to strip it down reasonably: https://ox.cx/stuff/sscce.tbz2


    You need a postgres DB that you have to configure in test.yml.

    This DB needs a single table:

    CREATE TABLE "public"."tokens" (
        "token" text NOT NULL COLLATE "default",
        "system" text NOT NULL COLLATE "default",
        "purpose" text NOT NULL COLLATE "default",
        "user_id" int4 NOT NULL,
        "created" timestamp(6) WITH TIME ZONE DEFAULT timezone('UTC'::text, now()),
        "valid" bool NOT NULL DEFAULT true


    • create a Python 3.5 venv
    • pip install -r dev-requirements.txt
    • run make

    You should be seeing src/tokens/aio.py 25 0 6 1 97% 34->35.

    If you add a XXX in that line though, the test will explode.

    Let me know if you need something.

  3. Hynek Schlawack reporter

    Sadly, 4.1b1 seems worse.

    I’ve updated, did an coverage erase, and run coverage run -m pytest.

    I’m getting the following output:

    Name                     Stmts   Miss Branch BrPart  Cover   Missing
    src/tokens/__init__.py       3      0      0      0   100%
    src/tokens/_common.py       14      0      2      0   100%
    src/tokens/aio.py           26      0     14      3    92%   17->-5, 34->-23, 36->-23
    src/tokens/rnd.py           20      0      4      0   100%
    TOTAL                       63      0     20      3    96%
    • line 17 is the docstring of a class definition,
    • line 34 is the context manager as before,
    • and 36 is an except clause.

    In htmlcov:

    2016-01-11 at 07.52.png

  4. Ned Batchelder repo owner

    Hmm, the class docstring is confusing me: I had this happen in code of mine, and the first change I made after b1 was to change it again. About the await's: it looks like I need to understand them better. I'll try your code.

  5. Hynek Schlawack reporter

    I’m on the ML and I saw the thread. Sadly my understanding of asyncio is still rather superficial ATM so I don’t have much to add. :(

  6. Hynek Schlawack reporter

    I can confirm that the current default branch fixes all asyncio problems I was aware of! Thanks Ned!

  7. Hynek Schlawack reporter

    OK I’m afraid I’ve found another similar case:

    except Something:
       raise  # <-- can't  get this covered

    If you wonder what that construct is good for: I want to whitelist certain exceptions that can pass unaltered but wrap all others.

    Should I reopen or open a new issue?

  8. Log in to comment