coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Assume `if TYPE_CHECKING: ... else: ...` block is covered

Open retnikt opened this issue 4 years ago • 7 comments

if typing.TYPE_CHECKING blocks are never run but are still counted as needing test coverage. ( https://docs.python.org/3.7/library/typing.html#typing.TYPE_CHECKING )

Example

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    # coverage.py thinks this is not covered because TYPE_CHECKING is True at runtime
    ...
else:
    ....

You have to add # pragma: no_cover to the if block here because coverage.py assumes TYPE_CHECKING to be False, and therefore assumes one of the blocks does not have a test case. It would be good if you didn't have to, and blocks requiring TYPE_CHECKING to be True are assumed to not need test cases.

retnikt avatar Aug 02 '19 15:08 retnikt

You can solve this yourself today by adding this to your .coveragerc file:

[report]
exclude_lines = 
    pragma: no cover
    if TYPE_CHECKING:

More details about this setting are here: Advanced exclusion.

Since this is something that is configurable by the user, I would rather not change coverage.py to deal with it.

nedbat avatar Aug 02 '19 17:08 nedbat

@nedbat - when would you expect code inside if TYPE_CHECKING blocks to be executed while code coverage measurement is in effect? How would you feel about making this part of the default exclude_lines?

cjw296 avatar Jan 24 '22 17:01 cjw296

A few years later, I think you are right that it should be part of the default. If the user wants to measure those lines, they can.

nedbat avatar Jan 24 '22 19:01 nedbat

Can we also add ... to the defaults while we're at it?

ikonst avatar Apr 06 '22 14:04 ikonst

@ikonst - I'm not 100% sure that's a good idea, but also as I've painfully discovered, you really mean \.\.\..

cjw296 avatar Apr 07 '22 07:04 cjw296

Yeah, obviously in its escaped form. I think the sharp change in coverage would tell you :)

ikonst avatar Apr 07 '22 14:04 ikonst

It can be quite the footgun when your projects are starting from 100% line coverage...

cjw296 avatar Apr 07 '22 17:04 cjw296

A few years later, I think you are right that it should be part of the default. If the user wants to measure those lines, they can.

@nedbat Is the else from a if not TYPE_CHECKING: supposed to be supported as well? If yes, any tips where I can implement this part?

Kludex avatar Nov 09 '22 19:11 Kludex