Skip to content

Commit 57b975d

Browse files
committed
types: explicit Protocol inheritance permits changing parameter names
ty didn't consider TreeMatcher to be an instance of TMatcher because the argument names differ.
1 parent 63ec12d commit 57b975d

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

coverage/files.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from coverage import env
1818
from coverage.exceptions import ConfigError
1919
from coverage.misc import human_sorted, isolate_module, join_regex
20+
from coverage.types import TMatcher
2021

2122
os = isolate_module(os)
2223

@@ -214,7 +215,7 @@ def prep_patterns(patterns: Iterable[str]) -> list[str]:
214215
return prepped
215216

216217

217-
class TreeMatcher:
218+
class TreeMatcher(TMatcher):
218219
"""A matcher for files in a tree.
219220
220221
Construct with a list of paths, either files or directories. Paths match
@@ -235,7 +236,7 @@ def info(self) -> list[str]:
235236
"""A list of strings for displaying when dumping state."""
236237
return self.original_paths
237238

238-
def match(self, fpath: str) -> bool:
239+
def match(self, fpath: str) -> bool: # pylint: disable=arguments-renamed
239240
"""Does `fpath` indicate a file in one of our trees?"""
240241
fpath = os.path.normcase(fpath)
241242
for p in self.paths:
@@ -249,7 +250,7 @@ def match(self, fpath: str) -> bool:
249250
return False
250251

251252

252-
class ModuleMatcher:
253+
class ModuleMatcher(TMatcher):
253254
"""A matcher for modules in a tree."""
254255

255256
def __init__(self, module_names: Iterable[str], name: str = "unknown") -> None:
@@ -263,7 +264,7 @@ def info(self) -> list[str]:
263264
"""A list of strings for displaying when dumping state."""
264265
return self.modules
265266

266-
def match(self, module_name: str) -> bool:
267+
def match(self, module_name: str) -> bool: # pylint: disable=arguments-renamed
267268
"""Does `module_name` indicate a module in one of our packages?"""
268269
if not module_name:
269270
return False
@@ -279,7 +280,7 @@ def match(self, module_name: str) -> bool:
279280
return False
280281

281282

282-
class GlobMatcher:
283+
class GlobMatcher(TMatcher):
283284
"""A matcher for files by file name pattern."""
284285

285286
def __init__(self, pats: Iterable[str], name: str = "unknown") -> None:
@@ -294,7 +295,7 @@ def info(self) -> list[str]:
294295
"""A list of strings for displaying when dumping state."""
295296
return self.pats
296297

297-
def match(self, fpath: str) -> bool:
298+
def match(self, fpath: str) -> bool: # pylint: disable=arguments-renamed
298299
"""Does `fpath` match one of our file name patterns?"""
299300
return self.re.match(fpath) is not None
300301

coverage/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class TFileDisposition(Protocol):
6565
has_dynamic_filename: bool
6666

6767

68+
class TMatcher(Protocol):
69+
"""The shape all Matchers have."""
70+
71+
def match(self, s: str) -> bool:
72+
"""Does this string match?"""
73+
74+
6875
# When collecting data, we use a dictionary with a few possible shapes. The
6976
# keys are always file names.
7077
# - If measuring line coverage, the values are sets of line numbers.

tests/test_files.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os.path
1111
import re
1212

13-
from typing import Any, Protocol
13+
from typing import Any
1414
from collections.abc import Iterable
1515
from unittest import mock
1616

@@ -29,6 +29,7 @@
2929
flat_rootname,
3030
globs_to_regex,
3131
)
32+
from coverage.types import TMatcher
3233

3334
from tests.coveragetest import CoverageTest
3435
from tests.helpers import os_sep
@@ -361,13 +362,6 @@ def test_invalid_globs(pattern: str, bad_word: str) -> None:
361362
globs_to_regex([pattern])
362363

363364

364-
class TMatcher(Protocol):
365-
"""The shape all Matchers have."""
366-
367-
def match(self, s: str) -> bool:
368-
"""Does this string match?"""
369-
370-
371365
class MatcherTest(CoverageTest):
372366
"""Tests of file matchers."""
373367

0 commit comments

Comments
 (0)