diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2022-12-31 19:04:31 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-12-31 20:39:09 -0500 |
commit | e3c523c98cbc9ecdd37e36da19848ca9d0aef4a3 (patch) | |
tree | 0857c3472c5858695d26f0276e8f1c5b3512a3e2 /coverage/context.py | |
parent | 5a72a1eb736516759201b223463f69f00979818e (diff) | |
download | python-coveragepy-git-e3c523c98cbc9ecdd37e36da19848ca9d0aef4a3.tar.gz |
mypy: add __init__.py, __main__.py, bytecode.py, context.py, exceptions.py, report.py, and version.py
Diffstat (limited to 'coverage/context.py')
-rw-r--r-- | coverage/context.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/coverage/context.py b/coverage/context.py index 6bb1f1ee..3b8bc10f 100644 --- a/coverage/context.py +++ b/coverage/context.py @@ -3,8 +3,13 @@ """Determine contexts for coverage.py""" +from types import FrameType +from typing import cast, Callable, Optional, Sequence -def combine_context_switchers(context_switchers): + +def combine_context_switchers( + context_switchers: Sequence[Callable[[FrameType], Optional[str]]], +) -> Optional[Callable[[FrameType], Optional[str]]]: """Create a single context switcher from multiple switchers. `context_switchers` is a list of functions that take a frame as an @@ -23,7 +28,7 @@ def combine_context_switchers(context_switchers): if len(context_switchers) == 1: return context_switchers[0] - def should_start_context(frame): + def should_start_context(frame: FrameType) -> Optional[str]: """The combiner for multiple context switchers.""" for switcher in context_switchers: new_context = switcher(frame) @@ -34,7 +39,7 @@ def combine_context_switchers(context_switchers): return should_start_context -def should_start_context_test_function(frame): +def should_start_context_test_function(frame: FrameType) -> Optional[str]: """Is this frame calling a test_* function?""" co_name = frame.f_code.co_name if co_name.startswith("test") or co_name == "runTest": @@ -42,7 +47,7 @@ def should_start_context_test_function(frame): return None -def qualname_from_frame(frame): +def qualname_from_frame(frame: FrameType) -> Optional[str]: """Get a qualified name for the code running in `frame`.""" co = frame.f_code fname = co.co_name @@ -55,11 +60,11 @@ def qualname_from_frame(frame): func = frame.f_globals.get(fname) if func is None: return None - return func.__module__ + "." + fname + return cast(str, func.__module__ + "." + fname) func = getattr(method, "__func__", None) if func is None: cls = self.__class__ - return cls.__module__ + "." + cls.__name__ + "." + fname + return cast(str, cls.__module__ + "." + cls.__name__ + "." + fname) - return func.__module__ + "." + func.__qualname__ + return cast(str, func.__module__ + "." + func.__qualname__) |