From 57ea8a13deff247e5929c6f63aeb9e8cee011dc2 Mon Sep 17 00:00:00 2001 From: Nick Drozd Date: Sun, 5 Jan 2025 07:38:24 -0600 Subject: [PATCH] type add some typing changes required by Mypyc (#1658) * Add Final annotations * Add TLineNo annotations * Use single assignment for class var --- coverage/config.py | 5 +++-- coverage/debug.py | 6 +++--- coverage/env.py | 9 +++------ coverage/parser.py | 2 +- coverage/results.py | 2 +- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/coverage/config.py b/coverage/config.py index 357fc5af0..748ad425a 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -13,7 +13,7 @@ import re from typing import ( - Any, Callable, Union, + Any, Callable, Final, Union, ) from collections.abc import Iterable @@ -360,7 +360,8 @@ def copy(self) -> CoverageConfig: """Return a copy of the configuration.""" return copy.deepcopy(self) - CONCURRENCY_CHOICES = {"thread", "gevent", "greenlet", "eventlet", "multiprocessing"} + CONCURRENCY_CHOICES: Final[set[str]] = { + "thread", "gevent", "greenlet", "eventlet", "multiprocessing"} CONFIG_FILE_OPTIONS = [ # These are *args for _set_attr_from_config_option: diff --git a/coverage/debug.py b/coverage/debug.py index cf9310dc5..2e578a3d4 100644 --- a/coverage/debug.py +++ b/coverage/debug.py @@ -21,7 +21,7 @@ from typing import ( overload, - Any, Callable, IO, + Any, Callable, Final, IO, ) from collections.abc import Iterable, Iterator, Mapping @@ -467,8 +467,8 @@ def get_one( # a process-wide singleton. So stash it in sys.modules instead of # on a class attribute. Yes, this is aggressively gross. - SYS_MOD_NAME = "$coverage.debug.DebugOutputFile.the_one" - SINGLETON_ATTR = "the_one_and_is_interim" + SYS_MOD_NAME: Final[str] = "$coverage.debug.DebugOutputFile.the_one" + SINGLETON_ATTR: Final[str] = "the_one_and_is_interim" @classmethod def _set_singleton_data(cls, the_one: DebugOutputFile, interim: bool) -> None: diff --git a/coverage/env.py b/coverage/env.py index 0fb8683c5..0944d64d5 100644 --- a/coverage/env.py +++ b/coverage/env.py @@ -9,7 +9,7 @@ import platform import sys -from typing import Any +from typing import Any, Final from collections.abc import Iterable # debug_info() at the bottom wants to show all the globals, but not imports. @@ -53,10 +53,7 @@ class PYBEHAVIOR: # Is "if not __debug__" optimized away? The exact details have changed # across versions. - if pep626: - optimize_if_not_debug = 1 - else: - optimize_if_not_debug = 2 + optimize_if_not_debug = 1 if pep626 else 2 # 3.7 changed how functions with only docstrings are numbered. docstring_only_function = (not PYPY) and (PYVERSION <= (3, 10)) @@ -148,7 +145,7 @@ class PYBEHAVIOR: soft_keywords = (PYVERSION >= (3, 10)) # PEP669 Low Impact Monitoring: https://peps.python.org/pep-0669/ - pep669 = bool(getattr(sys, "monitoring", None)) + pep669: Final[bool] = bool(getattr(sys, "monitoring", None)) # Where does frame.f_lasti point when yielding from a generator? # It used to point at the YIELD, in 3.13 it points at the RESUME, diff --git a/coverage/parser.py b/coverage/parser.py index fb74ea9e0..e5d9adcde 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -466,7 +466,7 @@ def _line_numbers(self) -> Iterable[TLineNo]: byte_increments = self.code.co_lnotab[0::2] line_increments = self.code.co_lnotab[1::2] - last_line_num = None + last_line_num: TLineNo | None = None line_num = self.code.co_firstlineno byte_num = 0 for byte_incr, line_incr in zip(byte_increments, line_increments): diff --git a/coverage/results.py b/coverage/results.py index a9bde97c3..74de21dbe 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -343,7 +343,7 @@ def _line_ranges( lines = sorted(lines) pairs = [] - start = None + start: TLineNo | None = None lidx = 0 for stmt in statements: if lidx >= len(lines):