coveragepy
coveragepy copied to clipboard
Excluded decorators in excluded branches leak into the outer indentation.
Describe the bug Coverage supports excluding coverage on branches and on function decorators. Alone these features work, but together they cause a leak of the exclusion into the outer indentation, excluding everything up to the next branch. See the minimal example.
To Reproduce
- What version of Python are you using?
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Python versions 3.8 and later tested with CI.
- What version of coverage.py shows the problem? The output of
coverage debug sys
is helpful.
coverage_version: 7.2.6
- What versions of what packages do you have installed? The output of
pip freeze
is helpful.
$ pip freeze
coverage==7.2.6
- What code shows the problem? Give us a specific commit of a specific repo that we can check out. If you've already worked around the problem, please provide a commit before that fix.
Real example of affected code, the exclusion leaks to the entire class below: https://github.com/HexDecimal/python-tcod-ecs/blob/cb0ad6a3b562ceb68a8596d6fd1564b2b3ca8de2/tcod/ecs/init.py#L248-L262 Issue visible in Codecov: https://app.codecov.io/gh/HexDecimal/python-tcod-ecs/commit/cb0ad6a3b562ceb68a8596d6fd1564b2b3ca8de2/blob/tcod/ecs/init.py#L248
Minimal example:
# pyproject.toml
[tool.coverage.report]
exclude_lines = ["if TYPE_CHECKING:", "@overload"] # Issue triggers only with both of these exclusions.
# sample.py
from typing import TYPE_CHECKING, overload
class Test:
if TYPE_CHECKING:
@overload
def test() -> None:
...
if True: # Branch excluded!
pass
if True: # Included
pass
if TYPE_CHECKING:
@overload
def test() -> None:
...
if True: # Branch excluded!
pass
if True: # Included
pass
- What commands should we run to reproduce the problem? Be specific. Include everything, even
git clone
,pip install
, and so on. Explain like we're five!
With the above two files in the working directory:
coverage run sample.py
coverage html
Expected behavior Excluded branches should overlap without covering areas outside of either.
Additional context
I can workaround this issue if I remove @overload
from the excludes and exclude ...
instead, this excludes everything I want:
[tool.coverage.report]
exclude_lines = ['^\s*\.\.\.', "if TYPE_CHECKING:"]