Coverage gets confused when encoding comment is in the wrong place

Issue #443 resolved
David MacIver created an issue

It seems that code which incorrectly has things before the encoding comment confuses coverage.


from __future__ import division, print_function, absolute_import

# coding=utf-8

class UniqueIdentifier(object):

    def __init__(self):

not_set = UniqueIdentifier()

import conventions

Then running the following:

python -m coverage run && coverage report --show-missing

Causes coverage to report that the file has not got 100% coverage (it does), but also gives nonsensical line numbers:

Name                  Stmts   Miss  Cover   Missing
---------------------------------------------------            5      3    40%   4, 7-10       1      0   100%   
TOTAL                     6      3    50%   

Line 4 is the encoding comment, and lines 7-10 correspond to the class body.

I have tested this on Python 3.4.3 running coverage 4.0.2. I noticed this when my CI started breaking (I erroneously had some code like this due to a confused formatter) on a coverage upgrade, so I think it's probably introduced in a recent version.

Comments (2)

  1. Ned Batchelder repo owner
    • changed status to open

    It's definitely reproducible, and it definitely appeared in 4.0.2. Thanks for the edge cases!

  2. Log in to comment