Partial branch coverage: `pragma: no cover` does ignore `pass`

Issue #483 closed
Mischa Krüger created an issue

Hi :)

I have discovered some behaviour that I think should be changed:

if conditionA:
    print("A")
elif conditionB:
    print("B")
else:  # pragma: no cover
    pass

I have the problem that the else clause is never reached (because I do some pre-checks). I tried to use # pragma: no cover like above, but I still get partial branch coverage. I need to insert a dummy statement like print or else so coverage takes it up correctly.

So this works:

if conditionA:
    print("A")
elif conditionB:
    print("B")
else:  # pragma: no cover
    print("never reached")

I'm using coverage 4.0.3, and as I've seen there's already 4.1 out. Maybe this has been already resolved, but I couldn't find an issue^^

Thanks in advance :D

Comments (6)

  1. Ned Batchelder repo owner

    I can't reproduce what you're seeing:

    [:~] $ mktmpenv -n
    New python executable in tmp-6fbd17cc551d313c/bin/python
    Installing setuptools, pip, wheel...done.
    This is a temporary environment. It will be deleted when you run 'deactivate'.
    [:~] [tmp-6fbd17cc551d313c] $ pip install coverage==4.03
    Collecting coverage==4.03
      Could not find a version that satisfies the requirement coverage==4.03 (from versions: 3.0b3, 3.0, 3.0.1, 3.1b1, 3.1, 3.2b1, 3.2b2, 3.2b3, 3.2b4, 3.2, 3.3, 3.3.1, 3.4b1, 3.4b2, 3.4, 3.5b1, 3.5, 3.5.1b1, 3.5.1, 3.5.2b1, 3.5.2, 3.5.3, 3.6b1, 3.6b2, 3.6b3, 3.6, 3.7, 3.7.1, 4.0a1, 4.0a2, 4.0a3, 4.0a4, 4.0a5, 4.0a6, 4.0b1, 4.0b2, 4.0b3, 4.0, 4.0.1, 4.0.2, 4.0.3, 4.1b1, 4.1b2)
    No matching distribution found for coverage==4.03
    You are using pip version 7.1.2, however version 8.1.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    [:~] [tmp-6fbd17cc551d313c] 1 $ pip install coverage==4.0.3
    Collecting coverage==4.0.3
    Installing collected packages: coverage
    Successfully installed coverage-4.0.3
    You are using pip version 7.1.2, however version 8.1.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    [:~] [tmp-6fbd17cc551d313c] $ coverage run --branch coverage/trunk/bug483.py
    A
    B
    [:~] [tmp-6fbd17cc551d313c] $ coverage report -m
    Name                       Stmts   Miss Branch BrPart  Cover   Missing
    ----------------------------------------------------------------------
    coverage/trunk/bug483.py       7      0      2      0   100%
    [:~] [tmp-6fbd17cc551d313c] $ more coverage/trunk/bug483.py
    def f(conditionA, conditionB):
        if conditionA:
            print("A")
        elif conditionB:
            print("B")
        else:  # pragma: no cover
            pass
    
    f(True, False)
    f(False, True)
    

    The coverage is 100%, with lines 6-7 excluded because of the pragma.

  2. Mischa Krüger reporter

    Indeed with my given snippet it doesn't work, but with this one:

    covbug.py

    from TestComponent import x
    
    print(x(0))
    print(x(1))
    print(x(2))
    

    TestComponent.py

    def x(a):
        # Just add arbitrary numbers...
        w = 5
        if a == 1:
            w += 7
        elif a == 2:
            w += 88
        else:
            return 0
    
        if a == 1:
            w += 1
        elif a == 2:
            w += 2
        else:  # pragma: no cover
            pass
    
        return w
    

    yields:

    D:\Users\MAK\Eigene Dokumente\GitHub> mkdir covbug
    D:\Users\MAK\Eigene Dokumente\GitHub> cd .\covbug
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> touch covbug.py
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> touch TestComponent.py
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> notepad .\covbug.py
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> notepad .\TestComponent.py
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> # Filled in contents above
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> coverage run --branch .\covbug.py
    0
    13
    95
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> coverage report -m
    Name               Stmts   Miss Branch BrPart  Cover   Missing
    --------------------------------------------------------------
    TestComponent.py      12      0      8      1    95%   13->18
    covbug.py              4      0      0      0   100%
    --------------------------------------------------------------
    TOTAL                 16      0      8      1    96%
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug>
    

    But replacing pass statement with e.g. list() yields:

    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> coverage run --branch .\covbug.py
    0
    13
    95
    D:\Users\MAK\Eigene Dokumente\GitHub\covbug> coverage report -m
    Name               Stmts   Miss Branch BrPart  Cover   Missing
    --------------------------------------------------------------
    TestComponent.py      12      0      6      0   100%
    covbug.py              4      0      0      0   100%
    --------------------------------------------------------------
    TOTAL                 16      0      6      0   100%
    
  3. Ned Batchelder repo owner

    OK, thanks, I see that behavior. This is because Python doesn't retain anything of lines 15 and 16 in the compiled code if line 16 is "pass". By the time coverage analyzes the code, lines 15 and 16 might as well be comments, so it doesn't know to include them in the analysis of possible branches.

    Having "else: pass" seems like a temporary thing, how important is it to fix this?

  4. Mischa Krüger reporter

    As there's a workaround (just replace pass with something else, like object() or some other statement), it's not that important. I've linked my portion of code to this issue + doing this workaround :)

    Thanks again :D

  5. Log in to comment