updates to add type hinting to uses of Callable

This commit is contained in:
Blake Harnden 2020-01-15 11:56:23 -08:00
parent b3118513fa
commit dcabd8d6f8
9 changed files with 22 additions and 18 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -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.

View file

@ -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.

View file

@ -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