From 76f07898d95bbb0d5580b58b085a7536eec5fc0c Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 1 Jul 2021 14:52:52 -0400 Subject: [PATCH] Mypy fixes to enforce uid tuple --- tractor/_actor.py | 25 +++++++++++++++---------- tractor/_ipc.py | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/tractor/_actor.py b/tractor/_actor.py index fac73ce..9d0b667 100644 --- a/tractor/_actor.py +++ b/tractor/_actor.py @@ -203,7 +203,7 @@ class Actor: enable_modules: List[str] = [], uid: str = None, loglevel: str = None, - arbiter_addr: Optional[Tuple[str, int]] = (None, None), + arbiter_addr: Optional[Tuple[str, int]] = None, spawn_method: Optional[str] = None ) -> None: """This constructor is called in the parent actor **before** the spawning @@ -233,7 +233,8 @@ class Actor: # TODO: consider making this a dynamically defined # @dataclass once we get py3.7 self.loglevel = loglevel - self._arb_addr = tuple(arbiter_addr) + + self._arb_addr = arbiter_addr or (None, None) # marked by the process spawning backend at startup # will be None for the parent most process started manually @@ -263,7 +264,7 @@ class Actor: self._parent_chan: Optional[Channel] = None self._forkserver_info: Optional[ Tuple[Any, Any, Any, Any, Any]] = None - self._actoruid2nursery: Dict[str, 'ActorNursery'] = {} # type: ignore + self._actoruid2nursery: Dict[str, 'ActorNursery'] = {} # type: ignore # noqa async def wait_for_peer( self, uid: Tuple[str, str] @@ -668,7 +669,8 @@ class Actor: # XXX: msgspec doesn't support serializing tuples # so just cash manually here since it's what our # internals expect. - self._arb_addr = tuple(value) + address: Tuple[str, int] = value + self._arb_addr = value else: setattr(self, attr, value) @@ -1043,6 +1045,7 @@ class Actor: async def _do_handshake( self, chan: Channel + ) -> Tuple[str, str]: """Exchange (name, UUIDs) identifiers as the first communication step. @@ -1050,10 +1053,10 @@ class Actor: parlance. """ await chan.send(self.uid) - uid: Tuple[str, str] = tuple(await chan.recv()) + uid: Tuple[str, str] = await chan.recv() - # if not isinstance(uid, tuple): - # raise ValueError(f"{uid} is not a valid uid?!") + if not isinstance(uid, tuple): + raise ValueError(f"{uid} is not a valid uid?!") chan.uid = uid log.info(f"Handshake with actor {uid}@{chan.raddr} complete") @@ -1118,10 +1121,12 @@ class Arbiter(Actor): return sockaddrs async def register_actor( - self, uid: Tuple[str, str], sockaddr: Tuple[str, int] + self, + uid: Tuple[str, str], + sockaddr: Tuple[str, str] + ) -> None: - uid = tuple(uid) - name, uuid = uid + name, uuid = tuple(uid) self._registry[uid] = tuple(sockaddr) # pop and signal all waiter events diff --git a/tractor/_ipc.py b/tractor/_ipc.py index 88f9d2d..e3fde67 100644 --- a/tractor/_ipc.py +++ b/tractor/_ipc.py @@ -138,7 +138,7 @@ class MsgspecTCPStream(MsgpackTCPStream): size, = struct.unpack(" None: async with self._send_lock: - bytes_data = self.encode(data) + bytes_data: bytes = self.encode(data) # supposedly the fastest says, # https://stackoverflow.com/a/54027962 - size: int = struct.pack("