Skip to content

Coverage on overload stubs for typing #970

Open

Description

@adithyabsk

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

Activity

  1. added
    bug
    on Apr 12, 2020
  2. nedbat commented on Apr 12, 2020

    @nedbat
    Member

    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.

  3. adithyabsk commented on Apr 12, 2020

    @adithyabsk
    Author

    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.

  4. The-Compiler commented on Jan 10, 2023

    @The-Compiler

    Would it perhaps make sense to add @overload to the default exclude_lines? They can't contain any code:

    At runtime, calling a @overload-decorated function directly will raise NotImplementedError.

    I suppose a downside would be that this would also catch @overload usage where overload is not actually typing.overload though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugexclude

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions