Multiline asserts need too many pragma: no covers

Issue #461 resolved
Barry Warsaw created an issue

Code like the following:

        assert command_class.name not in config.commands, (  # pragma: no cover
            'Duplicate email command "{}" found in {}'.format( # pragma: no cover
                command_class.name,                            # pragma: no cover
                command_class.__module__))

Note the pragma: no cover on every line. I want to completely ignore the entire assert statement, but w/o all those pragmas, I get a report that some of those lines aren't covered, which is true, but unhelpful!

Comments (11)

  1. Ned Batchelder repo owner

    @warsaw can you provide a complete runnable example? Is the statement not covered because of -O?

  2. Ned Batchelder repo owner

    This was my attempt to reproduce it:

    $ cat bug461.py
    a = 1
    assert not a, (
          'Duplicate email command "{}" found in {}'.format(
                    len([]),
                    len([1])))
    b = 2
    print(a, b)
    $ PYTHONOPTIMIZE=1 coverage run bug461.py
    (1, 2)
    $ coverage report -m
    Name        Stmts   Miss  Cover   Missing
    -----------------------------------------
    bug461.py       4      1    75%   2
    $ vim bug461.py
    $ cat bug461.py
    a = 1
    assert not a, (                                             # pragma: no cover
          'Duplicate email command "{}" found in {}'.format(
                    len([]),
                    len([1])))
    b = 2
    print(a, b)
    $ PYTHONOPTIMIZE=1 coverage run bug461.py
    (1, 2)
    $ coverage report -m
    Name        Stmts   Miss  Cover   Missing
    -----------------------------------------
    bug461.py       3      0   100%
    

    Here only line 2 is considered a statement, and a single pragma was enough to quiet it.

  3. Barry Warsaw reporter

    That's interesting. In my larger test case, I'm not running with -O, and it seems like all four lines in the assert are being counted (and thus all four need pragma: no cover). I'll have to see if I can boil down to a smaller reproducible test case.

  4. Ned Batchelder repo owner

    If it doesn't reduce down, you can give me instructions how to run the larger test case.

  5. Barry Warsaw reporter

    Hi Ned, thanks for the help. You should be able to reproduce like so:

    $ git clone https://gitlab.com/mailman/mailman.git
    $ cd mailman
    $ tox -e coverage
    

    You might have to change basepython to python3.4 in tox.ini if you don't have Python 3.5. I'm fairly certain it's not a Python 3.4/3.5 thing, but I can check that.

    There are several cases where multiline asserts are being treated as multiple statements and being reported as not covered. src/mailman/app/commands.py is a good example. Note that I tried # pragma: no cover on these lines, but the current git head uses an exclude_lines in coverage.ini, which the tox.ini arranges to be used.

  6. Ned Batchelder repo owner

    OK, I've reproduced your results in your repo, thanks. Now to figure out why it's different than my test case....

  7. Barry Warsaw reporter

    Oh gosh. Too funny, and yeah probably so! I really should get rid of those, but it's a lot of legacy code to go through. Thanks for working on the fix! (I should take a crack at the Python bug.)

  8. Log in to comment