Skip to content

Commit

Permalink
mypy: test_debug.py test_execfile.py test_filereporter.py test_files.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 5, 2023
1 parent 7b48747 commit 3a02703
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 144 deletions.
20 changes: 11 additions & 9 deletions coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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, TConfigSection, TConfigValue
from coverage.types import (
TConfigurable, TConfigSectionIn, TConfigValueIn, TConfigSectionOut, TConfigValueOut,
)

os = isolate_module(os)

Expand Down Expand Up @@ -71,9 +73,9 @@ def options(self, section: str) -> List[str]:
return super().options(real_section)
raise ConfigError(f"No section: {section!r}")

def get_section(self, section: str) -> TConfigSection:
def get_section(self, section: str) -> TConfigSectionOut:
"""Get the contents of a section, as a dictionary."""
d: Dict[str, TConfigValue] = {}
d: Dict[str, TConfigValueOut] = {}
for opt in self.options(section):
d[opt] = self.get(section, opt)
return d
Expand Down Expand Up @@ -249,15 +251,15 @@ def __init__(self) -> None:
self.paths: Dict[str, List[str]] = {}

# Options for plugins
self.plugin_options: Dict[str, TConfigSection] = {}
self.plugin_options: Dict[str, TConfigSectionOut] = {}

MUST_BE_LIST = {
"debug", "concurrency", "plugins",
"report_omit", "report_include",
"run_omit", "run_include",
}

def from_args(self, **kwargs: TConfigValue) -> None:
def from_args(self, **kwargs: TConfigValueIn) -> None:
"""Read config values from `kwargs`."""
for k, v in kwargs.items():
if v is not None:
Expand Down Expand Up @@ -441,11 +443,11 @@ def _set_attr_from_config_option(
return True
return False

def get_plugin_options(self, plugin: str) -> TConfigSection:
def get_plugin_options(self, plugin: str) -> TConfigSectionOut:
"""Get a dictionary of options for the plugin named `plugin`."""
return self.plugin_options.get(plugin, {})

def set_option(self, option_name: str, value: Union[TConfigValue, TConfigSection]) -> None:
def set_option(self, option_name: str, value: Union[TConfigValueIn, TConfigSectionIn]) -> None:
"""Set an option in the configuration.
`option_name` is a colon-separated string indicating the section and
Expand Down Expand Up @@ -476,7 +478,7 @@ def set_option(self, option_name: str, value: Union[TConfigValue, TConfigSection
# If we get here, we didn't find the option.
raise ConfigError(f"No such option: {option_name!r}")

def get_option(self, option_name: str) -> Optional[TConfigValue]:
def get_option(self, option_name: str) -> Optional[TConfigValueOut]:
"""Get an option from the configuration.
`option_name` is a colon-separated string indicating the section and
Expand Down Expand Up @@ -559,7 +561,7 @@ def config_files_to_try(config_file: Union[bool, str]) -> List[Tuple[str, bool,
def read_coverage_config(
config_file: Union[bool, str],
warn: Callable[[str], None],
**kwargs: TConfigValue,
**kwargs: TConfigValueIn,
) -> CoverageConfig:
"""Read the coverage.py configuration.
Expand Down
21 changes: 11 additions & 10 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@
from coverage.results import Analysis
from coverage.summary import SummaryReporter
from coverage.types import (
TConfigurable, TConfigSection, TConfigValue, TFileDisposition, TLineNo, TMorf,
TConfigurable, TConfigSectionIn, TConfigValueIn, TConfigValueOut,
TFileDisposition, TLineNo, TMorf,
)
from coverage.xmlreport import XmlReporter


os = isolate_module(os)

@contextlib.contextmanager
def override_config(cov: Coverage, **kwargs: TConfigValue) -> Generator[None, None, None]:
def override_config(cov: Coverage, **kwargs: TConfigValueIn) -> Generator[None, None, None]:
"""Temporarily tweak the configuration of `cov`.
The arguments are applied to `cov.config` with the `from_args` method.
Expand Down Expand Up @@ -120,12 +121,12 @@ def __init__( # pylint: disable=too-many-arguments
timid: Optional[bool]=None,
branch: Optional[bool]=None,
config_file: Union[str, bool]=True,
source: Optional[List[str]]=None,
source_pkgs: Optional[List[str]]=None,
omit: Optional[Union[str, List[str]]]=None,
include: Optional[Union[str, List[str]]]=None,
debug: Optional[List[str]]=None,
concurrency: Optional[Union[str, List[str]]]=None,
source: Optional[Iterable[str]]=None,
source_pkgs: Optional[Iterable[str]]=None,
omit: Optional[Union[str, Iterable[str]]]=None,
include: Optional[Union[str, Iterable[str]]]=None,
debug: Optional[Iterable[str]]=None,
concurrency: Optional[Union[str, Iterable[str]]]=None,
check_preimported: bool=False,
context: Optional[str]=None,
messages: bool=False,
Expand Down Expand Up @@ -425,7 +426,7 @@ def _message(self, msg: str) -> None:
if self._messages:
print(msg)

def get_option(self, option_name: str) -> Optional[TConfigValue]:
def get_option(self, option_name: str) -> Optional[TConfigValueOut]:
"""Get an option from the configuration.
`option_name` is a colon-separated string indicating the section and
Expand All @@ -443,7 +444,7 @@ def get_option(self, option_name: str) -> Optional[TConfigValue]:
"""
return self.config.get_option(option_name)

def set_option(self, option_name: str, value: Union[TConfigValue, TConfigSection]) -> None:
def set_option(self, option_name: str, value: Union[TConfigValueIn, TConfigSectionIn]) -> None:
"""Set an option in the configuration.
`option_name` is a colon-separated string indicating the section and
Expand Down
2 changes: 1 addition & 1 deletion coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _glob_to_regex(pattern: str) -> str:
def globs_to_regex(
patterns: Iterable[str],
case_insensitive: bool=False,
partial: bool=False
partial: bool=False,
) -> re.Pattern[str]:
"""Convert glob patterns to a compiled regex that matches any of them.
Expand Down
8 changes: 4 additions & 4 deletions coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from coverage import env
from coverage.exceptions import ConfigError
from coverage.misc import import_third_party, substitute_variables
from coverage.types import TConfigSection, TConfigValue
from coverage.types import TConfigSectionOut, TConfigValueOut


if env.PYVERSION >= (3, 11, 0, "alpha", 7):
Expand Down Expand Up @@ -65,7 +65,7 @@ def read(self, filenames: Iterable[str]) -> List[str]:
raise ConfigError(msg.format(filename))
return []

def _get_section(self, section: str) -> Tuple[Optional[str], Optional[TConfigSection]]:
def _get_section(self, section: str) -> Tuple[Optional[str], Optional[TConfigSectionOut]]:
"""Get a section from the data.
Arguments:
Expand All @@ -92,7 +92,7 @@ def _get_section(self, section: str) -> Tuple[Optional[str], Optional[TConfigSec
return None, None
return real_section, data

def _get(self, section: str, option: str) -> Tuple[str, TConfigValue]:
def _get(self, section: str, option: str) -> Tuple[str, TConfigValueOut]:
"""Like .get, but returns the real section name and the value."""
name, data = self._get_section(section)
if data is None:
Expand Down Expand Up @@ -135,7 +135,7 @@ def options(self, section: str) -> List[str]:
raise ConfigError(f"No section: {section!r}")
return list(data.keys())

def get_section(self, section: str) -> TConfigSection:
def get_section(self, section: str) -> TConfigSectionOut:
_, data = self._get_section(section)
return data or {}

Expand Down
10 changes: 6 additions & 4 deletions coverage/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ def get_stats(self) -> Optional[Dict[str, int]]:
## Configuration

# One value read from a config file.
TConfigValue = Optional[Union[bool, int, float, str, List[str]]]
TConfigValueIn = Optional[Union[bool, int, float, str, Iterable[str]]]
TConfigValueOut = Optional[Union[bool, int, float, str, List[str]]]
# An entire config section, mapping option names to values.
TConfigSection = Mapping[str, TConfigValue]
TConfigSectionIn = Mapping[str, TConfigValueIn]
TConfigSectionOut = Mapping[str, TConfigValueOut]

class TConfigurable(Protocol):
"""Something that can proxy to the coverage configuration settings."""

def get_option(self, option_name: str) -> Optional[TConfigValue]:
def get_option(self, option_name: str) -> Optional[TConfigValueOut]:
"""Get an option from the configuration.
`option_name` is a colon-separated string indicating the section and
Expand All @@ -125,7 +127,7 @@ def get_option(self, option_name: str) -> Optional[TConfigValue]:
"""

def set_option(self, option_name: str, value: Union[TConfigValue, TConfigSection]) -> None:
def set_option(self, option_name: str, value: Union[TConfigValueIn, TConfigSectionIn]) -> None:
"""Set an option in the configuration.
`option_name` is a colon-separated string indicating the section and
Expand Down
10 changes: 5 additions & 5 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from coverage.control import DEFAULT_DATAFILE
from coverage.config import CoverageConfig
from coverage.exceptions import _ExceptionDuringRun
from coverage.types import TConfigValue
from coverage.types import TConfigValueIn, TConfigValueOut
from coverage.version import __url__

from tests.coveragetest import CoverageTest, OK, ERR, command_line
Expand Down Expand Up @@ -96,7 +96,7 @@ def model_object(self) -> mock.Mock:
def mock_command_line(
self,
args: str,
options: Optional[Mapping[str, TConfigValue]]=None,
options: Optional[Mapping[str, TConfigValueIn]]=None,
) -> Tuple[mock.Mock, int]:
"""Run `args` through the command line, with a Mock.
Expand Down Expand Up @@ -130,7 +130,7 @@ def cmd_executes(
args: str,
code: str,
ret: int=OK,
options: Optional[Mapping[str, TConfigValue]]=None,
options: Optional[Mapping[str, TConfigValueIn]]=None,
) -> None:
"""Assert that the `args` end up executing the sequence in `code`."""
called, status = self.mock_command_line(args, options=options)
Expand Down Expand Up @@ -1139,10 +1139,10 @@ def __init__(
self.json_result = json_report
self.lcov_result = lcov_result

def set_option(self, optname: str, optvalue: TConfigValue) -> None:
def set_option(self, optname: str, optvalue: TConfigValueIn) -> None:
self.config.set_option(optname, optvalue)

def get_option(self, optname: str) -> TConfigValue:
def get_option(self, optname: str) -> TConfigValueOut:
return self.config.get_option(optname)

def load(self) -> None:
Expand Down

0 comments on commit 3a02703

Please sign in to comment.