Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage on overload stubs for typing #970

Open
adithyabsk opened this issue Apr 12, 2020 · 3 comments
Open

Coverage on overload stubs for typing #970

adithyabsk opened this issue Apr 12, 2020 · 3 comments
Labels
bug Something isn't working exclude

Comments

@adithyabsk
Copy link

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
@adithyabsk adithyabsk added the bug Something isn't working label Apr 12, 2020
@nedbat
Copy link
Owner

nedbat commented Apr 12, 2020

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.

@adithyabsk
Copy link
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.

@The-Compiler
Copy link

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.

jkoelndorfer added a commit to jkoelndorfer/d2lfg that referenced this issue Jan 12, 2024
coverage.py does not currently handle @overload decorated methods
gracefully. Because they are not allowed to contain code, their method
bodies should be ignored.

See nedbat/coveragepy#970 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working exclude
Projects
None yet
Development

No branches or pull requests

3 participants