From 41ba50443de5f0094f7783b952260895528668fc Mon Sep 17 00:00:00 2001 From: Abhinav Singh <126065+abhinavsingh@users.noreply.github.com> Date: Thu, 13 Jan 2022 18:55:07 +0530 Subject: [PATCH] Type fixes (#979) * Type fixes * Type fix * Py class role * unused any import --- .gitignore | 1 - codecov.yml | 1 + docs/conf.py | 1 + proxy/core/work/pool.py | 10 +++++----- proxy/core/work/remote.py | 1 - proxy/core/work/threaded.py | 8 +++++--- proxy/core/work/threadless.py | 14 +++++++++----- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 09bd9363..410b2226 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,4 @@ htmlcov dist build -proxy/public profile.svg diff --git a/codecov.yml b/codecov.yml index d120bd49..904bc771 100644 --- a/codecov.yml +++ b/codecov.yml @@ -12,6 +12,7 @@ coverage: # target: 100% # we always want 100% coverage here paths: - "tests/" # only include coverage in "tests/" folder + threshold: 1% lib: # declare a new status context "lib" paths: - "!tests/" # remove all files in "tests/" diff --git a/docs/conf.py b/docs/conf.py index 2bd5ff7d..8543ee80 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -312,6 +312,7 @@ nitpick_ignore = [ (_py_class_role, 'Work'), (_py_class_role, 'proxy.core.acceptor.work.Work'), (_py_class_role, 'connection.Connection'), + (_py_class_role, 'EventQueue'), (_py_obj_role, 'proxy.core.work.threadless.T'), (_py_obj_role, 'proxy.core.work.work.T'), ] diff --git a/proxy/core/work/pool.py b/proxy/core/work/pool.py index 197cceeb..066b6441 100644 --- a/proxy/core/work/pool.py +++ b/proxy/core/work/pool.py @@ -13,16 +13,16 @@ import argparse import multiprocessing from multiprocessing import connection - -from typing import Any, Optional, List +from typing import TYPE_CHECKING, Any, Optional, List from .remote import RemoteExecutor -from ..event import EventQueue - from ...common.flag import flags from ...common.constants import DEFAULT_NUM_WORKERS, DEFAULT_THREADLESS +if TYPE_CHECKING: + from ..event import EventQueue + logger = logging.getLogger(__name__) @@ -71,7 +71,7 @@ class ThreadlessPool: def __init__( self, flags: argparse.Namespace, - event_queue: Optional[EventQueue] = None, + event_queue: Optional['EventQueue'] = None, ) -> None: self.flags = flags self.event_queue = event_queue diff --git a/proxy/core/work/remote.py b/proxy/core/work/remote.py index 8fe4ed45..bc16e83e 100644 --- a/proxy/core/work/remote.py +++ b/proxy/core/work/remote.py @@ -12,7 +12,6 @@ import asyncio import logging from typing import Optional, Any - from multiprocessing import connection from multiprocessing.reduction import recv_handle diff --git a/proxy/core/work/threaded.py b/proxy/core/work/threaded.py index 7404b44e..b9e9c913 100644 --- a/proxy/core/work/threaded.py +++ b/proxy/core/work/threaded.py @@ -11,13 +11,15 @@ import socket import argparse import threading -from typing import Optional, Tuple -from .work import Work +from typing import TYPE_CHECKING, Optional, Tuple from ..connection import TcpClientConnection from ..event import EventQueue, eventNames +if TYPE_CHECKING: + from .work import Work + def start_threaded_work( flags: argparse.Namespace, @@ -25,7 +27,7 @@ def start_threaded_work( addr: Optional[Tuple[str, int]], event_queue: Optional[EventQueue] = None, publisher_id: Optional[str] = None, -) -> Tuple[Work[TcpClientConnection], threading.Thread]: +) -> Tuple['Work[TcpClientConnection]', threading.Thread]: """Utility method to start a work in a new thread.""" work = flags.work_klass( TcpClientConnection(conn, addr), diff --git a/proxy/core/work/threadless.py b/proxy/core/work/threadless.py index 5abe0b10..d56cfe27 100644 --- a/proxy/core/work/threadless.py +++ b/proxy/core/work/threadless.py @@ -18,7 +18,7 @@ import selectors import multiprocessing from abc import abstractmethod, ABC -from typing import Any, Dict, Optional, Tuple, List, Set, Generic, TypeVar, Union +from typing import TYPE_CHECKING, Dict, Optional, Tuple, List, Set, Generic, TypeVar, Union from ...common.logger import Logger from ...common.types import Readables, SelectableEvents, Writables @@ -26,9 +26,13 @@ from ...common.constants import DEFAULT_INACTIVE_CONN_CLEANUP_TIMEOUT, DEFAULT_S from ...common.constants import DEFAULT_WAIT_FOR_TASKS_TIMEOUT from ..connection import TcpClientConnection, UpstreamConnectionPool -from ..event import eventNames, EventQueue +from ..event import eventNames -from .work import Work +if TYPE_CHECKING: + from typing import Any + + from ..event import EventQueue + from .work import Work T = TypeVar('T') @@ -62,7 +66,7 @@ class Threadless(ABC, Generic[T]): iid: str, work_queue: T, flags: argparse.Namespace, - event_queue: Optional[EventQueue] = None, + event_queue: Optional['EventQueue'] = None, ) -> None: super().__init__() self.iid = iid @@ -71,7 +75,7 @@ class Threadless(ABC, Generic[T]): self.event_queue = event_queue self.running = multiprocessing.Event() - self.works: Dict[int, Work[Any]] = {} + self.works: Dict[int, 'Work[Any]'] = {} self.selector: Optional[selectors.DefaultSelector] = None # If we remove single quotes for typing hint below, # runtime exceptions will occur for < Python 3.9.