diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2023-01-12 20:51:58 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2023-01-12 21:04:26 -0500 |
commit | b5b223723fd1aeaa8ed650b8440b4b8c08f8e378 (patch) | |
tree | 3fcf638c95544992e80c1523c1b6084dec05ad93 /tests | |
parent | a5aeec363e2f50312007ef40903f1dcaae0b5133 (diff) | |
download | python-coveragepy-git-b5b223723fd1aeaa8ed650b8440b4b8c08f8e378.tar.gz |
mypy: all of coverage/ and tests/ are checked
Diffstat (limited to 'tests')
-rw-r--r-- | tests/modules/plugins/a_plugin.py | 10 | ||||
-rw-r--r-- | tests/modules/plugins/another.py | 11 | ||||
-rw-r--r-- | tests/modules/process_test/try_execfile.py | 14 | ||||
-rw-r--r-- | tests/plugin1.py | 35 | ||||
-rw-r--r-- | tests/plugin2.py | 40 | ||||
-rw-r--r-- | tests/plugin_config.py | 15 |
6 files changed, 88 insertions, 37 deletions
diff --git a/tests/modules/plugins/a_plugin.py b/tests/modules/plugins/a_plugin.py index 0cc96e5a..2122e869 100644 --- a/tests/modules/plugins/a_plugin.py +++ b/tests/modules/plugins/a_plugin.py @@ -1,11 +1,19 @@ """A plugin for tests to reference.""" +from __future__ import annotations + +from typing import Any + from coverage import CoveragePlugin +from coverage.plugin_support import Plugins class Plugin(CoveragePlugin): pass -def coverage_init(reg, options): +def coverage_init( + reg: Plugins, + options: Any, # pylint: disable=unused-argument +) -> None: reg.add_file_tracer(Plugin()) diff --git a/tests/modules/plugins/another.py b/tests/modules/plugins/another.py index dfe03c97..a6145903 100644 --- a/tests/modules/plugins/another.py +++ b/tests/modules/plugins/another.py @@ -3,12 +3,19 @@ """A plugin for tests to reference.""" -from coverage import CoveragePlugin +from __future__ import annotations + +from typing import Any +from coverage import CoveragePlugin +from coverage.plugin_support import Plugins class Plugin(CoveragePlugin): pass -def coverage_init(reg, options): +def coverage_init( + reg: Plugins, + options: Any, # pylint: disable=unused-argument +) -> None: reg.add_file_tracer(Plugin()) diff --git a/tests/modules/process_test/try_execfile.py b/tests/modules/process_test/try_execfile.py index 2c741662..ad97a23b 100644 --- a/tests/modules/process_test/try_execfile.py +++ b/tests/modules/process_test/try_execfile.py @@ -20,17 +20,21 @@ differences and get a clean diff. """ +from __future__ import annotations + import itertools import json import os import sys +from typing import Any, List + # sys.path varies by execution environments. Coverage.py uses setuptools to # make console scripts, which means pkg_resources is imported. pkg_resources # removes duplicate entries from sys.path. So we do that too, since the extra # entries don't affect the running of the program. -def same_file(p1, p2): +def same_file(p1: str, p2: str) -> bool: """Determine if `p1` and `p2` refer to the same existing file.""" if not p1: return not p2 @@ -45,9 +49,9 @@ def same_file(p1, p2): norm2 = os.path.normcase(os.path.normpath(p2)) return norm1 == norm2 -def without_same_files(filenames): +def without_same_files(filenames: List[str]) -> List[str]: """Return the list `filenames` with duplicates (by same_file) removed.""" - reduced = [] + reduced: List[str] = [] for filename in filenames: if not any(same_file(filename, other) for other in reduced): reduced.append(filename) @@ -59,7 +63,7 @@ DATA = "xyzzy" import __main__ -def my_function(a): +def my_function(a: Any) -> str: """A function to force execution of module-level values.""" return f"my_fn({a!r})" @@ -71,7 +75,7 @@ spec = globals().get('__spec__') # A more compact ad-hoc grouped-by-first-letter list of builtins. CLUMPS = "ABC,DEF,GHI,JKLMN,OPQR,ST,U,VWXYZ_,ab,cd,efg,hij,lmno,pqr,stuvwxyz".split(",") -def word_group(w): +def word_group(w: str) -> int: """Figure out which CLUMP the first letter of w is in.""" for i, clump in enumerate(CLUMPS): if w[0] in clump: diff --git a/tests/plugin1.py b/tests/plugin1.py index 3283fbda..4848eaff 100644 --- a/tests/plugin1.py +++ b/tests/plugin1.py @@ -3,28 +3,34 @@ """A file tracer plugin for test_plugins.py to import.""" +from __future__ import annotations + import os.path -import coverage +from types import FrameType +from typing import Any, Optional, Set, Tuple, Union +from coverage import CoveragePlugin, FileReporter, FileTracer +from coverage.plugin_support import Plugins +from coverage.types import TLineNo -class Plugin(coverage.CoveragePlugin): +class Plugin(CoveragePlugin): """A file tracer plugin to import, so that it isn't in the test's current directory.""" - def file_tracer(self, filename): + def file_tracer(self, filename: str) -> Optional[FileTracer]: """Trace only files named xyz.py""" if "xyz.py" in filename: - return FileTracer(filename) + return MyFileTracer(filename) return None - def file_reporter(self, filename): - return FileReporter(filename) + def file_reporter(self, filename: str) -> Union[FileReporter, str]: + return MyFileReporter(filename) -class FileTracer(coverage.FileTracer): +class MyFileTracer(FileTracer): """A FileTracer emulating a simple static plugin.""" - def __init__(self, filename): + def __init__(self, filename: str) -> None: """Claim that */*xyz.py was actually sourced from /src/*ABC.zz""" self._filename = filename self._source_filename = os.path.join( @@ -32,21 +38,24 @@ class FileTracer(coverage.FileTracer): os.path.basename(filename.replace("xyz.py", "ABC.zz")) ) - def source_filename(self): + def source_filename(self) -> str: return self._source_filename - def line_number_range(self, frame): + def line_number_range(self, frame: FrameType) -> Tuple[TLineNo, TLineNo]: """Map the line number X to X05,X06,X07.""" lineno = frame.f_lineno return lineno*100+5, lineno*100+7 -class FileReporter(coverage.FileReporter): +class MyFileReporter(FileReporter): """Dead-simple FileReporter.""" - def lines(self): + def lines(self) -> Set[TLineNo]: return {105, 106, 107, 205, 206, 207} -def coverage_init(reg, options): # pylint: disable=unused-argument +def coverage_init( + reg: Plugins, + options: Any, # pylint: disable=unused-argument +) -> None: """Called by coverage to initialize the plugins here.""" reg.add_file_tracer(Plugin()) diff --git a/tests/plugin2.py b/tests/plugin2.py index 60d16206..5cb8fbb6 100644 --- a/tests/plugin2.py +++ b/tests/plugin2.py @@ -3,9 +3,16 @@ """A file tracer plugin for test_plugins.py to import.""" +from __future__ import annotations + import os.path -import coverage +from types import FrameType +from typing import Any, Optional, Set, Tuple + +from coverage import CoveragePlugin, FileReporter, FileTracer +from coverage.plugin_support import Plugins +from coverage.types import TLineNo try: import third.render # pylint: disable=unused-import @@ -16,43 +23,50 @@ except ImportError: pass -class Plugin(coverage.CoveragePlugin): +class Plugin(CoveragePlugin): """A file tracer plugin for testing.""" - def file_tracer(self, filename): + def file_tracer(self, filename: str) -> Optional[FileTracer]: if "render.py" in filename: return RenderFileTracer() return None - def file_reporter(self, filename): - return FileReporter(filename) + def file_reporter(self, filename: str) -> FileReporter: + return MyFileReporter(filename) -class RenderFileTracer(coverage.FileTracer): +class RenderFileTracer(FileTracer): """A FileTracer using information from the caller.""" - def has_dynamic_source_filename(self): + def has_dynamic_source_filename(self) -> bool: return True - def dynamic_source_filename(self, filename, frame): + def dynamic_source_filename( + self, + filename: str, + frame: FrameType, + ) -> Optional[str]: if frame.f_code.co_name != "render": return None - source_filename = os.path.abspath(frame.f_locals['filename']) + source_filename: str = os.path.abspath(frame.f_locals['filename']) return source_filename - def line_number_range(self, frame): + def line_number_range(self, frame: FrameType) -> Tuple[TLineNo, TLineNo]: lineno = frame.f_locals['linenum'] return lineno, lineno+1 -class FileReporter(coverage.FileReporter): +class MyFileReporter(FileReporter): """A goofy file reporter.""" - def lines(self): + def lines(self) -> Set[TLineNo]: # Goofy test arrangement: claim that the file has as many lines as the # number in its name. num = os.path.basename(self.filename).split(".")[0].split("_")[1] return set(range(1, int(num)+1)) -def coverage_init(reg, options): # pylint: disable=unused-argument +def coverage_init( + reg: Plugins, + options: Any, # pylint: disable=unused-argument +) -> None: """Called by coverage to initialize the plugins here.""" reg.add_file_tracer(Plugin()) diff --git a/tests/plugin_config.py b/tests/plugin_config.py index c4d3cf65..bb6893e3 100644 --- a/tests/plugin_config.py +++ b/tests/plugin_config.py @@ -3,20 +3,29 @@ """A configuring plugin for test_plugins.py to import.""" +from __future__ import annotations + +from typing import Any, List, cast + import coverage +from coverage.plugin_support import Plugins +from coverage.types import TConfigurable class Plugin(coverage.CoveragePlugin): """A configuring plugin for testing.""" - def configure(self, config): + def configure(self, config: TConfigurable) -> None: """Configure all the things!""" opt_name = "report:exclude_lines" - exclude_lines = config.get_option(opt_name) + exclude_lines = cast(List[str], config.get_option(opt_name)) exclude_lines.append(r"pragma: custom") exclude_lines.append(r"pragma: or whatever") config.set_option(opt_name, exclude_lines) -def coverage_init(reg, options): # pylint: disable=unused-argument +def coverage_init( + reg: Plugins, + options: Any, # pylint: disable=unused-argument +) -> None: """Called by coverage to initialize the plugins here.""" reg.add_configurer(Plugin()) |