Coveragepy: Coverage on overload stubs for typing

Created on 12 Apr 2020  路  2Comments  路  Source: nedbat/coveragepy

Situation

Coverage marks @overload stubs as uncovered.

# scratch_2.py
from typing import overload, Union

@overload
def some_func(int_or_str: str) -> str:
    ...  # currently marked as uncovered

@overload
def some_func(int_or_str: int) -> int:
    ... # currently marked as uncovered

def some_func(int_or_str: Union[int, str]) -> Union[int, str]:
    return int_or_str

def test_some_func():
    assert 10 == some_func(10)
    assert "test" == some_func("test")
$ mypy scratch_2.py
$ pytest scratch_2.py --cov=scratch_2 --cov-report=term

Expected results

Full coverage

Current output

scratch_2.py .                                                                                                                                                                                     [100%]

---------- coverage: platform darwin, python 3.7.6-final-0 -----------
Name           Stmts   Miss  Cover
----------------------------------
scratch_2.py      12      2    83%

Versioning

Coverage.py, version 5.0.4 with C extension
Full documentation is at https://coverage.readthedocs.io
bug

Most helpful comment

Hi @nedbat thanks for the note, adding @overload to the coverage report exclusions is a great idea, instead of individually marking the lines as no cover.

It'd be great to use this issue to track progress on coverage being able to handle typing.

All 2 comments

You can configure coverage.py to ignore the overload functions by adding this to your .coveragerc file:

[report]
exclude_lines = 
    pragma: not covered
    @overload

(You need "pragma: not covered" there to keep the default pragma comment working.)

Perhaps a future version of coverage will know more about typing natively, but this will fix your project for the time-being.

Hi @nedbat thanks for the note, adding @overload to the coverage report exclusions is a great idea, instead of individually marking the lines as no cover.

It'd be great to use this issue to track progress on coverage being able to handle typing.

Was this page helpful?
0 / 5 - 0 ratings