From 870d1accc7862ae083f5b56769acb20e1e197384 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 8 Dec 2022 14:58:58 +0200 Subject: [PATCH] Use real types instead of comments --- hai/boto3_multipart_upload.py | 2 +- hai/event_emitter.py | 4 ++-- hai/parallel.py | 4 ++-- hai/pipe_pump.py | 12 ++++++------ hai/rate_limiter.py | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/hai/boto3_multipart_upload.py b/hai/boto3_multipart_upload.py index 80eb0ec..f90ff94 100644 --- a/hai/boto3_multipart_upload.py +++ b/hai/boto3_multipart_upload.py @@ -134,7 +134,7 @@ def upload_file( raise TypeError('`fp` must have a `read()` method') try: - size = os.stat(fp.fileno()).st_size # type: Optional[int] + size = os.stat(fp.fileno()).st_size except (OSError, AttributeError): size = None diff --git a/hai/event_emitter.py b/hai/event_emitter.py index cdd341f..5e2c63a 100644 --- a/hai/event_emitter.py +++ b/hai/event_emitter.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Dict, Optional, Set DICT_NAME = '_event_emitter_dict' @@ -10,7 +10,7 @@ def _get_event_emitter_dict(obj: Any) -> Dict[str, Any]: class EventEmitter: - event_types = set() # type: set[str] + event_types: Set[str] = set() def on(self, event: str, handler: Handler) -> None: if event != '*' and event not in self.event_types: diff --git a/hai/parallel.py b/hai/parallel.py index 9ed9d61..613bb59 100644 --- a/hai/parallel.py +++ b/hai/parallel.py @@ -45,8 +45,8 @@ class ParallelRun: def __init__(self, parallelism: Optional[int] = None) -> None: self.pool = ThreadPool(processes=(parallelism or (int(os.cpu_count() or 1) * 2))) self.task_complete_event = threading.Event() - self.tasks = [] # type: List[ApplyResult[Any]] - self.completed_tasks = WeakSet() # type: WeakSet[ApplyResult[Any]] + self.tasks: List[ApplyResult[Any]] = [] + self.completed_tasks: WeakSet[ApplyResult[Any]] = WeakSet() def __enter__(self) -> "ParallelRun": return self diff --git a/hai/pipe_pump.py b/hai/pipe_pump.py index a54b821..8a68799 100644 --- a/hai/pipe_pump.py +++ b/hai/pipe_pump.py @@ -12,7 +12,7 @@ class BasePipePump: def __init__(self) -> None: self.selector = selectors.DefaultSelector() - self.buffers = {} # type: Dict[str, bytes] + self.buffers: Dict[str, bytes] = {} self.selector_lock = threading.Lock() def register(self, key: str, fileobj: Optional[IO[bytes]]) -> None: @@ -46,7 +46,7 @@ def pump(self, timeout: float = 0, max_reads: int = 1) -> int: read_num += 1 should_repeat = False for (key, _event) in self.selector.select(timeout=timeout): - fileobj = key.fileobj # type: IO[bytes] # type: ignore[assignment] + fileobj: IO[bytes] = key.fileobj # type: ignore[assignment] data = fileobj.read(self.read_size) self.feed(key.data, data) should_repeat = True # Got data, should try again @@ -125,8 +125,8 @@ def __init__(self, separator: bytes = b'\n') -> None: super().__init__() assert isinstance(separator, bytes) self.separator = separator - self.lines = {} # type: Dict[str, List[bytes]] - self._line_handlers = [] # type: List[LineHandler] + self.lines: Dict[str, List[bytes]] = {} + self._line_handlers: List[LineHandler] = [] def add_line_handler(self, handler: LineHandler) -> None: """ @@ -241,8 +241,8 @@ class CRLFPipePump(BasePipePump): def __init__(self) -> None: super().__init__() - self.line_state = {} # type: Dict[str, Tuple[Optional[bytes], bool]] - self._handlers = [] # type: List[CRLFHandler] + self.line_state: Dict[str, Tuple[Optional[bytes], bool]] = {} + self._handlers: List[CRLFHandler] = [] def add_handler(self, handler: CRLFHandler) -> None: """ diff --git a/hai/rate_limiter.py b/hai/rate_limiter.py index bcdd183..45e1d3b 100644 --- a/hai/rate_limiter.py +++ b/hai/rate_limiter.py @@ -91,9 +91,9 @@ def __init__(self, rate: Rate, allow_underflow: bool = False) -> None: """ self.rate = rate self.allow_underflow = bool(allow_underflow) - self.last_check = None # type: Optional[float] - self.allowance = None # type: Optional[float] - self.current_state = None # type: Optional[bool] + self.last_check: Optional[float] = None + self.allowance: Optional[float] = None + self.current_state: Optional[bool] = None @classmethod def from_per_second(cls, per_second: int, allow_underflow: bool = False) -> "RateLimiter": @@ -155,7 +155,7 @@ class MultiRateLimiter: allow_underflow = False def __init__(self, default_limit: Rate, per_name_limits: Optional[Dict[str, Rate]] = None) -> None: - self.limiters = {} # type: Dict[str, RateLimiter] + self.limiters: Dict[str, RateLimiter] = {} self.default_limit = default_limit self.per_name_limits = dict(per_name_limits or {}) assert isinstance(default_limit, Rate)