Skip to content

Commit

Permalink
mypy: add env.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 1, 2023
1 parent 0bf14e2 commit 09f9188
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def post_process(self) -> None:
for k, v in self.paths.items()
)

def debug_info(self) -> List[Tuple[str, str]]:
def debug_info(self) -> Iterable[Tuple[str, Any]]:
"""Make a list of (name, value) pairs for writing debug info."""
return human_sorted_items( # type: ignore
(k, v) for k, v in self.__dict__.items() if not k.startswith("_")
Expand Down
6 changes: 2 additions & 4 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
from coverage.report import render_report
from coverage.results import Analysis
from coverage.summary import SummaryReporter
from coverage.types import (
TConfigurable, TConfigSection, TConfigValue, TLineNo, TMorf, TSysInfo,
)
from coverage.types import TConfigurable, TConfigSection, TConfigValue, TLineNo, TMorf
from coverage.xmlreport import XmlReporter


Expand Down Expand Up @@ -1233,7 +1231,7 @@ def lcov_report(
):
return render_report(self.config.lcov_output, LcovReporter(self), morfs, self._message)

def sys_info(self) -> TSysInfo:
def sys_info(self) -> Iterable[Tuple[str, Any]]:
"""Return a list of (key, value) pairs showing internal information."""

import coverage as covmod
Expand Down
10 changes: 8 additions & 2 deletions coverage/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import types
import _thread

from typing import Any, Callable, Iterable, Iterator, Tuple

from coverage.misc import isolate_module

os = isolate_module(os)
Expand Down Expand Up @@ -108,7 +110,7 @@ def info_header(label):
return "--{:-<60s}".format(" "+label+" ")


def info_formatter(info):
def info_formatter(info: Iterable[Tuple[str, Any]]) -> Iterator[str]:
"""Produce a sequence of formatted lines from info.
`info` is a sequence of pairs (label, data). The produced lines are
Expand All @@ -135,7 +137,11 @@ def info_formatter(info):
yield "%*s: %s" % (label_len, label, data)


def write_formatted_info(write, header, info):
def write_formatted_info(
write: Callable[[str], None],
header: str,
info: Iterable[Tuple[str, Any]],
) -> None:
"""Write a sequence of (label,data) pairs nicely.
`write` is a function write(str) that accepts each line of output.
Expand Down
9 changes: 6 additions & 3 deletions coverage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import platform
import sys

from typing import Any, Iterable, Tuple

# Operating systems.
WINDOWS = sys.platform == "win32"
LINUX = sys.platform.startswith("linux")
Expand All @@ -21,7 +23,7 @@
PYVERSION = sys.version_info + (int(platform.python_version()[-1] == "+"),)

if PYPY:
PYPYVERSION = sys.pypy_version_info
PYPYVERSION = sys.pypy_version_info # type: ignore[attr-defined]

# Python behavior.
class PYBEHAVIOR:
Expand Down Expand Up @@ -134,13 +136,14 @@ class PYBEHAVIOR:
TESTING = os.getenv('COVERAGE_TESTING', '') == 'True'


def debug_info():
def debug_info() -> Iterable[Tuple[str, Any]]:
"""Return a list of (name, value) pairs for printing debug information."""
info = [
(name, value) for name, value in globals().items()
if not name.startswith("_") and
name not in {"PYBEHAVIOR", "debug_info"} and
not isinstance(value, type(os))
not isinstance(value, type(os)) and
not str(value).startswith("typing.")
]
info += [
(name, value) for name, value in PYBEHAVIOR.__dict__.items()
Expand Down
6 changes: 3 additions & 3 deletions coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import traceback

from types import FrameType, ModuleType
from typing import cast, Iterable, List, Optional, Set, Tuple, TYPE_CHECKING
from typing import cast, Any, Iterable, List, Optional, Set, Tuple, TYPE_CHECKING

from coverage import env
from coverage.disposition import FileDisposition, disposition_init
Expand All @@ -25,7 +25,7 @@
from coverage.files import prep_patterns, find_python_files, canonical_filename
from coverage.misc import sys_modules_saved
from coverage.python import source_for_file, source_for_morf
from coverage.types import TMorf, TWarnFn, TDebugCtl, TSysInfo
from coverage.types import TMorf, TWarnFn, TDebugCtl

if TYPE_CHECKING:
from coverage.config import CoverageConfig
Expand Down Expand Up @@ -565,7 +565,7 @@ def _find_executable_files(self, src_dir: str) -> Iterable[Tuple[str, Optional[s
continue
yield file_path, plugin_name

def sys_info(self) -> TSysInfo:
def sys_info(self) -> Iterable[Tuple[str, Any]]:
"""Our information for Coverage.sys_info.
Returns a list of (key, value) pairs.
Expand Down
4 changes: 2 additions & 2 deletions coverage/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def coverage_init(reg, options):

from coverage import files
from coverage.misc import _needs_to_implement
from coverage.types import TArc, TConfigurable, TLineNo, TSourceTokenLines, TSysInfo
from coverage.types import TArc, TConfigurable, TLineNo, TSourceTokenLines


class CoveragePlugin:
Expand Down Expand Up @@ -235,7 +235,7 @@ def configure(self, config: TConfigurable) -> None:
"""
pass

def sys_info(self) -> TSysInfo:
def sys_info(self) -> Iterable[Tuple[str, Any]]:
"""Get a list of information useful for debugging.
Plug-in type: any.
Expand Down
5 changes: 1 addition & 4 deletions coverage/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from types import ModuleType
from typing import (
Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union,
Any, Dict, Iterable, List, Optional, Tuple, Union,
TYPE_CHECKING,
)

Expand Down Expand Up @@ -81,6 +81,3 @@ def should(self, option: str) -> bool:

def write(self, msg: str) -> None:
"""Write a line of debug output."""

# Data returned from sys_info()
TSysInfo = Sequence[Tuple[str, Union[str, Iterable[str]]]]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ deps =
setenv =
{[testenv]setenv}
C__B=coverage/__init__.py coverage/__main__.py coverage/annotate.py coverage/bytecode.py
C_CE=coverage/config.py coverage/context.py coverage/control.py coverage/data.py coverage/disposition.py coverage/exceptions.py
C_CE=coverage/config.py coverage/context.py coverage/control.py coverage/data.py coverage/disposition.py coverage/env.py coverage/exceptions.py
C_FN=coverage/files.py coverage/inorout.py coverage/jsonreport.py coverage/lcovreport.py coverage/multiproc.py coverage/numbits.py
C_OP=coverage/parser.py coverage/phystokens.py coverage/plugin.py coverage/python.py
C_QZ=coverage/report.py coverage/results.py coverage/sqldata.py coverage/tomlconfig.py coverage/types.py coverage/version.py
Expand Down

0 comments on commit 09f9188

Please sign in to comment.