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

Created on 2 Aug 2019  路  1Comment  路  Source: nedbat/coveragepy

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.

bug

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings