From b02de965ec19b8dd0203d60faef9fa1af6ced5a9 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 9 Aug 2023 06:45:02 -0400 Subject: [PATCH] test(refactor): abstract method compatible with both pytest and mypy Using Protocol as a base class introduces a __init__ method, which pytest does not like. This way keeps both tools happy. --- metacov.ini | 3 +++ tests/test_api.py | 19 +++++++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/metacov.ini b/metacov.ini index 884babf7e..84321712e 100644 --- a/metacov.ini +++ b/metacov.ini @@ -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 diff --git a/tests/test_api.py b/tests/test_api.py index cbc35b1af..dcdebe97d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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 @@ -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():