Enable mypy checking on V1 code (#11105)

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
This commit is contained in:
Mark McLoughlin
2024-12-14 09:54:04 -08:00
committed by GitHub
parent 93abf23a64
commit 6d917d0eeb
21 changed files with 160 additions and 121 deletions
+27 -15
View File
@@ -1,6 +1,8 @@
from collections import OrderedDict
from collections.abc import Sequence
from contextlib import contextmanager
from typing import Any, Generic, Iterator, List, TypeVar, overload
from typing import (Any, Generic, Iterator, List, Optional, TypeVar, Union,
overload)
import zmq
@@ -11,7 +13,7 @@ logger = init_logger(__name__)
T = TypeVar("T")
class ConstantList(Generic[T]):
class ConstantList(Generic[T], Sequence):
def __init__(self, x: List[T]) -> None:
self._x = x
@@ -34,29 +36,33 @@ class ConstantList(Generic[T]):
def clear(self):
raise Exception("Cannot clear a constant list")
def index(self, item):
return self._x.index(item)
def index(self,
item: T,
start: int = 0,
stop: Optional[int] = None) -> int:
return self._x.index(item, start,
stop if stop is not None else len(self._x))
@overload
def __getitem__(self, item) -> T:
def __getitem__(self, item: int) -> T:
...
@overload
def __getitem__(self, s: slice, /) -> List[T]:
...
def __getitem__(self, item):
def __getitem__(self, item: Union[int, slice]) -> Union[T, List[T]]:
return self._x[item]
@overload
def __setitem__(self, item, value):
def __setitem__(self, item: int, value: T):
...
@overload
def __setitem__(self, s: slice, value, /):
def __setitem__(self, s: slice, value: T, /):
...
def __setitem__(self, item, value):
def __setitem__(self, item: Union[int, slice], value: Union[T, List[T]]):
raise Exception("Cannot set item in a constant list")
def __delitem__(self, item):
@@ -73,10 +79,12 @@ class ConstantList(Generic[T]):
@contextmanager
def make_zmq_socket(path: str, type: Any) -> Iterator[zmq.Socket]:
def make_zmq_socket(
path: str,
type: Any) -> Iterator[zmq.Socket]: # type: ignore[name-defined]
"""Context manager for a ZMQ socket"""
ctx = zmq.Context()
ctx = zmq.Context() # type: ignore[attr-defined]
try:
socket = ctx.socket(type)
@@ -96,20 +104,24 @@ def make_zmq_socket(path: str, type: Any) -> Iterator[zmq.Socket]:
ctx.destroy(linger=0)
class LRUDictCache:
K = TypeVar('K')
V = TypeVar('V')
class LRUDictCache(Generic[K, V]):
def __init__(self, size: int):
self.cache = OrderedDict()
self.cache: OrderedDict[K, V] = OrderedDict()
self.size = size
def get(self, key, default=None):
def get(self, key: K, default=None) -> V:
if key not in self.cache:
return default
self.cache.move_to_end(key)
return self.cache[key]
def put(self, key, value):
def put(self, key: K, value: V):
self.cache[key] = value
self.cache.move_to_end(key)
if len(self.cache) > self.size: