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/plugin2.py | |
parent | a5aeec363e2f50312007ef40903f1dcaae0b5133 (diff) | |
download | python-coveragepy-git-b5b223723fd1aeaa8ed650b8440b4b8c08f8e378.tar.gz |
mypy: all of coverage/ and tests/ are checked
Diffstat (limited to 'tests/plugin2.py')
-rw-r--r-- | tests/plugin2.py | 40 |
1 files changed, 27 insertions, 13 deletions
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()) |