diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index 668e15ed..a2641e87 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -105,7 +105,7 @@ class InterfaceHelper: ) -def stream_listener(stream: Any, handler: Callable) -> None: +def stream_listener(stream: Any, handler: Callable[[core_pb2.Event], None]) -> None: """ Listen for stream events and provide them to the handler. @@ -123,7 +123,7 @@ def stream_listener(stream: Any, handler: Callable) -> None: logging.exception("stream error") -def start_streamer(stream: Any, handler: Callable) -> None: +def start_streamer(stream: Any, handler: Callable[[core_pb2.Event], None]) -> None: """ Convenience method for starting a grpc stream thread for handling streamed events. @@ -402,7 +402,10 @@ class CoreGrpcClient: return self.stub.AddSessionServer(request) def events( - self, session_id: int, handler: Callable, events: List[core_pb2.Event] = None + self, + session_id: int, + handler: Callable[[core_pb2.Event], None], + events: List[core_pb2.Event] = None, ) -> Any: """ Listen for session events. @@ -419,7 +422,9 @@ class CoreGrpcClient: start_streamer(stream, handler) return stream - def throughputs(self, session_id: int, handler: Callable) -> Any: + def throughputs( + self, session_id: int, handler: Callable[[core_pb2.ThroughputsEvent], None] + ) -> Any: """ Listen for throughput events with information for interfaces and bridges. diff --git a/daemon/core/emulator/distributed.py b/daemon/core/emulator/distributed.py index 5eb3cef6..105b767f 100644 --- a/daemon/core/emulator/distributed.py +++ b/daemon/core/emulator/distributed.py @@ -21,7 +21,6 @@ from core.nodes.network import CoreNetwork, CtrlNet if TYPE_CHECKING: from core.emulator.session import Session - LOCK = threading.Lock() CMD_HIDE = True @@ -139,7 +138,7 @@ class DistributedController: cmd = f"mkdir -p {self.session.session_dir}" server.remote_cmd(cmd) - def execute(self, func: Callable) -> None: + def execute(self, func: Callable[[DistributedServer], None]) -> None: """ Convenience for executing logic against all distributed servers. diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index f819c4c3..ca585c31 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -1206,7 +1206,7 @@ class Session: ExceptionLevels.ERROR, "Session.run_state_hooks", None, message ) - def add_state_hook(self, state: int, hook: Callable) -> None: + def add_state_hook(self, state: int, hook: Callable[[int], None]) -> None: """ Add a state hook. @@ -1222,7 +1222,7 @@ class Session: if self.state == state: hook(state) - def del_state_hook(self, state: int, hook: Callable) -> None: + def del_state_hook(self, state: int, hook: Callable[[int], None]) -> None: """ Delete a state hook. diff --git a/daemon/core/nodes/docker.py b/daemon/core/nodes/docker.py index 445fb3ec..1d7f3f02 100644 --- a/daemon/core/nodes/docker.py +++ b/daemon/core/nodes/docker.py @@ -16,7 +16,7 @@ if TYPE_CHECKING: class DockerClient: - def __init__(self, name: str, image: str, run: Callable) -> None: + def __init__(self, name: str, image: str, run: Callable[..., str]) -> None: self.name = name self.image = image self.run = run diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index aea3116d..3a5836af 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -366,7 +366,7 @@ class TunTap(CoreInterface): self.up = False def waitfor( - self, func: Callable, attempts: int = 10, maxretrydelay: float = 0.25 + self, func: Callable[[], int], attempts: int = 10, maxretrydelay: float = 0.25 ) -> bool: """ Wait for func() to return zero with exponential backoff. diff --git a/daemon/core/nodes/lxd.py b/daemon/core/nodes/lxd.py index 5e5dd927..20e2bc43 100644 --- a/daemon/core/nodes/lxd.py +++ b/daemon/core/nodes/lxd.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: class LxdClient: - def __init__(self, name: str, image: str, run: Callable) -> None: + def __init__(self, name: str, image: str, run: Callable[..., str]) -> None: self.name = name self.image = image self.run = run diff --git a/daemon/core/nodes/netclient.py b/daemon/core/nodes/netclient.py index e51285ec..1236468f 100644 --- a/daemon/core/nodes/netclient.py +++ b/daemon/core/nodes/netclient.py @@ -12,7 +12,7 @@ class LinuxNetClient: Client for creating Linux bridges and ip interfaces for nodes. """ - def __init__(self, run: Callable) -> None: + def __init__(self, run: Callable[..., str]) -> None: """ Create LinuxNetClient instance. @@ -360,7 +360,7 @@ class OvsNetClient(LinuxNetClient): self.run(f"{OVS_BIN} set bridge {name} other_config:mac-aging-time=0") -def get_net_client(use_ovs: bool, run: Callable) -> LinuxNetClient: +def get_net_client(use_ovs: bool, run: Callable[..., str]) -> LinuxNetClient: """ Retrieve desired net client for running network commands. diff --git a/daemon/core/nodes/network.py b/daemon/core/nodes/network.py index 9c21ba04..c7c36a1e 100644 --- a/daemon/core/nodes/network.py +++ b/daemon/core/nodes/network.py @@ -239,7 +239,7 @@ class EbtablesQueue: ebq = EbtablesQueue() -def ebtablescmds(call: Callable, cmds: List[str]) -> None: +def ebtablescmds(call: Callable[..., str], cmds: List[str]) -> None: """ Run ebtable commands. diff --git a/daemon/core/utils.py b/daemon/core/utils.py index 6b7129b6..0d7e4a33 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -167,7 +167,7 @@ def which(command: str, required: bool) -> str: return found_path -def make_tuple(obj: Any) -> Tuple[Any]: +def make_tuple(obj: Generic[T]) -> Tuple[T]: """ Create a tuple from an object, or return the object itself. @@ -181,7 +181,7 @@ def make_tuple(obj: Any) -> Tuple[Any]: return (obj,) -def make_tuple_fromstr(s: str, value_type: Callable) -> Tuple[Any]: +def make_tuple_fromstr(s: str, value_type: Callable[[str], T]) -> Tuple[T]: """ Create a tuple from a string. @@ -200,11 +200,11 @@ def make_tuple_fromstr(s: str, value_type: Callable) -> Tuple[Any]: return tuple(value_type(i) for i in values) -def mute_detach(args: List[str], **kwargs: Dict[str, Any]) -> int: +def mute_detach(args: str, **kwargs: Dict[str, Any]) -> int: """ Run a muted detached process by forking it. - :param list[str]|str args: arguments for the command + :param str args: arguments for the command :param dict kwargs: keyword arguments for the command :return: process id of the command :rtype: int