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 0251718 commit 1d11522
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
1 change: 1 addition & 0 deletions metacov.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exclude_lines =
# Not-real code for type checking
if TYPE_CHECKING:
class .*\(Protocol\):
pragma: for typing

# OS error conditions that we can't (or don't care to) replicate.
pragma: cant happen
Expand Down
17 changes: 6 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
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]:
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]: # pragma: for typing
"""Run coverage on usepkgs, return a line summary. kwargs are for Coverage(**kwargs)."""
return ""


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

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 1d11522

Please sign in to comment.