Skip to content

Commit

Permalink
test(refactor): abstract method compatible with both pytest and mypy
Browse files Browse the repository at this point in the history
Using Protocol as a base class introduces a __init__ method, which
pytest does not like.  This way keeps both tools happy.
  • Loading branch information
nedbat committed Aug 9, 2023
1 parent ada3ade commit b02de96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
3 changes: 3 additions & 0 deletions metacov.ini
Expand Up @@ -43,6 +43,9 @@ exclude_lines =
#
pragma: nested

# Abstract methods
raise NotImplementedError

# Lines that are only executed when we are debugging coverage.py.
def __repr__
pragma: debugging
Expand Down
19 changes: 7 additions & 12 deletions tests/test_api.py
Expand Up @@ -15,7 +15,7 @@
import sys
import textwrap

from typing import cast, Callable, Dict, Iterable, List, Optional, Protocol, Set
from typing import cast, Callable, Dict, Iterable, List, Optional, Set

import pytest

Expand Down Expand Up @@ -814,19 +814,14 @@ def test_bug_572(self) -> None:
cov.report()


class CoverageUsePkgs(Protocol):
"""A number of test classes have the same helper method."""
def coverage_usepkgs(
self,
**kwargs: TCovKwargs,
) -> Iterable[str]:
"""Run coverage on usepkgs, return a line summary. kwargs are for Coverage(**kwargs)."""
return ""


class IncludeOmitTestsMixin(CoverageUsePkgs, UsingModulesMixin, CoverageTest):
class IncludeOmitTestsMixin(UsingModulesMixin, CoverageTest):
"""Test methods for coverage methods taking include and omit."""

# An abstract method for subclasses to define, to appease mypy.
def coverage_usepkgs(self, **kwargs_unused: TCovKwargs) -> Iterable[str]:
"""Run coverage on usepkgs, return a line summary. kwargs are for Coverage(**kwargs)."""
raise NotImplementedError()

def filenames_in(self, summary: Iterable[str], filenames: str) -> None:
"""Assert the `filenames` are in the `summary`."""
for filename in filenames.split():
Expand Down

0 comments on commit b02de96

Please sign in to comment.