summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2022-12-29 17:02:20 -0500
committerNed Batchelder <ned@nedbatchelder.com>2022-12-29 17:29:50 -0500
commitbc83e9c53b810251ee11104eda1ee70772aeff72 (patch)
tree999a425237dadd74ac35861ae47fed8a825df6a9
parent21d66355a392d3d3dec8f79770e4be7673edf1dd (diff)
downloadpython-coveragepy-git-bc83e9c53b810251ee11104eda1ee70772aeff72.tar.gz
mypy: check multiproc.py
-rw-r--r--coverage/multiproc.py21
-rw-r--r--tox.ini2
2 files changed, 12 insertions, 11 deletions
diff --git a/coverage/multiproc.py b/coverage/multiproc.py
index 3a9bd633..e11ca7b7 100644
--- a/coverage/multiproc.py
+++ b/coverage/multiproc.py
@@ -10,7 +10,8 @@ import os.path
import sys
import traceback
-from coverage.misc import contract
+from typing import Any, Dict
+
# An attribute that will be set on the module to indicate that it has been
# monkey-patched.
@@ -18,12 +19,12 @@ PATCHED_MARKER = "_coverage$patched"
OriginalProcess = multiprocessing.process.BaseProcess
-original_bootstrap = OriginalProcess._bootstrap
+original_bootstrap = OriginalProcess._bootstrap # type: ignore[attr-defined]
class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-method
"""A replacement for multiprocess.Process that starts coverage."""
- def _bootstrap(self, *args, **kwargs):
+ def _bootstrap(self, *args, **kwargs): # type: ignore[no-untyped-def]
"""Wrapper around _bootstrap to start coverage."""
try:
from coverage import Coverage # avoid circular import
@@ -31,6 +32,7 @@ class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-m
cov._warn_preimported_source = False
cov.start()
debug = cov._debug
+ assert debug is not None
if debug.should("multiproc"):
debug.write("Calling multiprocessing bootstrap")
except Exception:
@@ -50,18 +52,17 @@ class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-m
class Stowaway:
"""An object to pickle, so when it is unpickled, it can apply the monkey-patch."""
- def __init__(self, rcfile):
+ def __init__(self, rcfile: str) -> None:
self.rcfile = rcfile
- def __getstate__(self):
+ def __getstate__(self) -> Dict[str, str]:
return {'rcfile': self.rcfile}
- def __setstate__(self, state):
+ def __setstate__(self, state: Dict[str, str]) -> None:
patch_multiprocessing(state['rcfile'])
-@contract(rcfile=str)
-def patch_multiprocessing(rcfile):
+def patch_multiprocessing(rcfile: str) -> None:
"""Monkey-patch the multiprocessing module.
This enables coverage measurement of processes started by multiprocessing.
@@ -74,7 +75,7 @@ def patch_multiprocessing(rcfile):
if hasattr(multiprocessing, PATCHED_MARKER):
return
- OriginalProcess._bootstrap = ProcessWithCoverage._bootstrap
+ OriginalProcess._bootstrap = ProcessWithCoverage._bootstrap # type: ignore[attr-defined]
# Set the value in ProcessWithCoverage that will be pickled into the child
# process.
@@ -92,7 +93,7 @@ def patch_multiprocessing(rcfile):
except (ImportError, AttributeError):
pass
else:
- def get_preparation_data_with_stowaway(name):
+ def get_preparation_data_with_stowaway(name: str) -> Dict[str, Any]:
"""Get the original preparation data, and also insert our stowaway."""
d = original_get_preparation_data(name)
d['stowaway'] = Stowaway(rcfile)
diff --git a/tox.ini b/tox.ini
index e1785719..4b641842 100644
--- a/tox.ini
+++ b/tox.ini
@@ -97,7 +97,7 @@ deps =
setenv =
{[testenv]setenv}
- T_AN=coverage/config.py coverage/files.py coverage/numbits.py
+ T_AN=coverage/config.py coverage/files.py coverage/multiproc.py coverage/numbits.py
T_OP=coverage/parser.py coverage/phystokens.py coverage/plugin.py coverage/python.py
T_QZ=coverage/results.py coverage/sqldata.py coverage/tomlconfig.py coverage/types.py
TYPEABLE={env:T_AN} {env:T_OP} {env:T_QZ}