From a15b90268f37d5f3a06b28ecb9277e636b493b1d Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 12 Jan 2023 10:47:08 -0500 Subject: mypy: progress on test_plugins.py --- coverage/config.py | 5 +++-- coverage/plugin_support.py | 7 ++++--- coverage/types.py | 7 +++++++ tests/test_plugins.py | 19 ++++++++++++------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/coverage/config.py b/coverage/config.py index ee30b8a4..046c1b00 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -20,7 +20,8 @@ from coverage.exceptions import ConfigError from coverage.misc import isolate_module, human_sorted_items, substitute_variables from coverage.tomlconfig import TomlConfigParser, TomlDecodeError from coverage.types import ( - TConfigurable, TConfigSectionIn, TConfigValueIn, TConfigSectionOut, TConfigValueOut, + TConfigurable, TConfigSectionIn, TConfigValueIn, TConfigSectionOut, + TConfigValueOut, TPluginConfig, ) os = isolate_module(os) @@ -166,7 +167,7 @@ DEFAULT_PARTIAL_ALWAYS = [ ] -class CoverageConfig(TConfigurable): +class CoverageConfig(TConfigurable, TPluginConfig): """Coverage.py configuration. The attributes of this class are the various settings that control the diff --git a/coverage/plugin_support.py b/coverage/plugin_support.py index 62985a06..4ed02c5c 100644 --- a/coverage/plugin_support.py +++ b/coverage/plugin_support.py @@ -12,11 +12,12 @@ import sys from types import FrameType from typing import Any, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Union -from coverage.config import CoverageConfig from coverage.exceptions import PluginError from coverage.misc import isolate_module from coverage.plugin import CoveragePlugin, FileTracer, FileReporter -from coverage.types import TArc, TConfigurable, TDebugCtl, TLineNo, TSourceTokenLines +from coverage.types import ( + TArc, TConfigurable, TDebugCtl, TLineNo, TPluginConfig, TSourceTokenLines, +) os = isolate_module(os) @@ -38,7 +39,7 @@ class Plugins: def load_plugins( cls, modules: Iterable[str], - config: CoverageConfig, + config: TPluginConfig, debug: Optional[TDebugCtl] = None, ) -> Plugins: """Load plugins from `modules`. diff --git a/coverage/types.py b/coverage/types.py index 736269e0..3d21ac9d 100644 --- a/coverage/types.py +++ b/coverage/types.py @@ -136,6 +136,13 @@ class TConfigurable(Protocol): """ +class TPluginConfig(Protocol): + """Something that can provide options to a plugin.""" + + def get_plugin_options(self, plugin: str) -> TConfigSectionOut: + """Get the options for a plugin.""" + + ## Parsing TMorf = Union[ModuleType, str] diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 866fab87..ab1f6b5f 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -3,10 +3,14 @@ """Tests for plugins.""" +from __future__ import annotations + import inspect import io import math import os.path + +from typing import Any, Dict, List from xml.etree import ElementTree import pytest @@ -17,6 +21,7 @@ from coverage.control import Plugins from coverage.data import line_counts, sorted_lines from coverage.exceptions import CoverageWarning, NoSource, PluginError from coverage.misc import import_local_file +from coverage.types import TConfigSectionOut, TPluginConfig import coverage.plugin @@ -24,18 +29,18 @@ from tests.coveragetest import CoverageTest from tests.helpers import CheckUniqueFilenames, swallow_warnings -class FakeConfig: +class FakeConfig(TPluginConfig): """A fake config for use in tests.""" - def __init__(self, plugin, options) -> None: + def __init__(self, plugin: str, options: Dict[str, Any]) -> None: self.plugin = plugin self.options = options - self.asked_for = [] + self.asked_for: List[str] = [] - def get_plugin_options(self, module): - """Just return the options for `module` if this is the right module.""" - self.asked_for.append(module) - if module == self.plugin: + def get_plugin_options(self, plugin: str) -> TConfigSectionOut: + """Just return the options for `plugin` if this is the right module.""" + self.asked_for.append(plugin) + if plugin == self.plugin: return self.options else: return {} -- cgit v1.2.1