Apply PR feedback: fix type hints

This commit is contained in:
NIC619 2019-07-30 15:31:02 +08:00
parent 2d4e23cfe2
commit e53727d301
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
15 changed files with 65 additions and 52 deletions

View File

@ -1,17 +1,17 @@
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
List,
Sequence,
)
import multiaddr
from libp2p.network.swarm import Swarm
from libp2p.network.network_interface import INetwork
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo
from libp2p.peer.peerstore import PeerStore
from libp2p.peer.peerstore_interface import IPeerStore
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
@ -24,17 +24,17 @@ from .host_interface import IHost
# telling it to listen on the given listen addresses.
StreamHandlerFn = Callable[[INetStream], Coroutine[Any, Any, None]]
StreamHandlerFn = Callable[[INetStream], Awaitable[None]]
class BasicHost(IHost):
_network: Swarm
_network: INetwork
router: KadmeliaPeerRouter
peerstore: PeerStore
peerstore: IPeerStore
# default options constructor
def __init__(self, network: Swarm, router: KadmeliaPeerRouter = None) -> None:
def __init__(self, network: INetwork, router: KadmeliaPeerRouter = None) -> None:
self._network = network
self._router = router
self.peerstore = self._network.peerstore
@ -45,13 +45,13 @@ class BasicHost(IHost):
"""
return self._network.get_peer_id()
def get_network(self) -> Swarm:
def get_network(self) -> INetwork:
"""
:return: network instance of host
"""
return self._network
def get_peerstore(self) -> PeerStore:
def get_peerstore(self) -> IPeerStore:
"""
:return: peerstore of the host (same one as in its network instance)
"""

View File

@ -1,22 +1,22 @@
from abc import ABC, abstractmethod
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
List,
Sequence,
)
import multiaddr
from libp2p.network.swarm import Swarm
from libp2p.network.network_interface import INetwork
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo
from libp2p.network.stream.net_stream_interface import INetStream
StreamHandlerFn = Callable[[INetStream], Coroutine[Any, Any, None]]
StreamHandlerFn = Callable[[INetStream], Awaitable[None]]
class IHost(ABC):
@ -28,7 +28,7 @@ class IHost(ABC):
"""
@abstractmethod
def get_network(self) -> Swarm:
def get_network(self) -> INetwork:
"""
:return: network instance of host
"""
@ -58,9 +58,9 @@ class IHost(ABC):
# protocol_id can be a list of protocol_ids
# stream will decide which protocol_id to run on
@abstractmethod
def new_stream(self,
async def new_stream(self,
peer_id: ID,
protocol_ids: Sequence[str]) -> Coroutine[Any, Any, INetStream]:
protocol_ids: Sequence[str]) -> INetStream:
"""
:param peer_id: peer_id that host is connecting
:param protocol_ids: protocol ids that stream can run on
@ -68,7 +68,7 @@ class IHost(ABC):
"""
@abstractmethod
def connect(self, peer_info: PeerInfo) -> Coroutine[Any, Any, None]:
async def connect(self, peer_info: PeerInfo) -> None:
"""
connect ensures there is a connection between this host and the peer with
given peer_info.peer_id. connect will absorb the addresses in peer_info into its internal

View File

@ -4,8 +4,9 @@ from abc import (
)
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
Dict,
Sequence,
TYPE_CHECKING,
)
@ -13,19 +14,25 @@ from typing import (
from multiaddr import Multiaddr
from libp2p.peer.id import ID
from libp2p.peer.peerstore import PeerStore
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from libp2p.transport.listener_interface import IListener
from .stream.net_stream import NetStream
from .stream.net_stream_interface import INetStream
if TYPE_CHECKING:
from .notifee_interface import INotifee
StreamHandlerFn = Callable[[NetStream], Coroutine[Any, Any, None]]
StreamHandlerFn = Callable[[INetStream], Awaitable[None]]
class INetwork(ABC):
peerstore: PeerStore
connections: Dict[ID, IMuxedConn]
listeners: Dict[str, IListener]
@abstractmethod
def get_peer_id(self) -> ID:
"""
@ -33,7 +40,7 @@ class INetwork(ABC):
"""
@abstractmethod
def dial_peer(self, peer_id: ID) -> Coroutine[Any, Any, IMuxedConn]:
async def dial_peer(self, peer_id: ID) -> IMuxedConn:
"""
dial_peer try to create a connection to peer_id
@ -51,9 +58,9 @@ class INetwork(ABC):
"""
@abstractmethod
def new_stream(self,
async def new_stream(self,
peer_id: ID,
protocol_ids: Sequence[str]) -> Coroutine[Any, Any, NetStream]:
protocol_ids: Sequence[str]) -> INetStream:
"""
:param peer_id: peer_id of destination
:param protocol_ids: available protocol ids to use for stream
@ -61,7 +68,7 @@ class INetwork(ABC):
"""
@abstractmethod
def listen(self, *args: Multiaddr) -> Coroutine[Any, Any, bool]:
async def listen(self, *args: Multiaddr) -> bool:
"""
:param *args: one or many multiaddrs to start listening on
:return: True if at least one success

View File

@ -1,16 +1,16 @@
from libp2p.stream_muxer.mplex.mplex import Mplex
from libp2p.stream_muxer.mplex.mplex_stream import MplexStream
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
from .net_stream_interface import INetStream
class NetStream(INetStream):
muxed_stream: MplexStream
mplex_conn: Mplex
muxed_stream: IMuxedStream
mplex_conn: IMuxedConn
protocol_id: str
def __init__(self, muxed_stream: MplexStream) -> None:
def __init__(self, muxed_stream: IMuxedStream) -> None:
self.muxed_stream = muxed_stream
self.mplex_conn = muxed_stream.mplex_conn
self.protocol_id = None

View File

@ -1,15 +1,14 @@
from abc import ABC, abstractmethod
from typing import (
Any,
Coroutine,
)
from libp2p.stream_muxer.mplex.mplex import Mplex
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
class INetStream(ABC):
mplex_conn: Mplex
mplex_conn: IMuxedConn
@abstractmethod
def get_protocol(self) -> str:
@ -25,21 +24,21 @@ class INetStream(ABC):
"""
@abstractmethod
def read(self) -> Coroutine[Any, Any, bytes]:
async def read(self) -> bytes:
"""
reads from the underlying muxed_stream
:return: bytes of input
"""
@abstractmethod
def write(self, data: bytes) -> Coroutine[Any, Any, int]:
async def write(self, data: bytes) -> int:
"""
write to the underlying muxed_stream
:return: number of bytes written
"""
@abstractmethod
def close(self) -> Coroutine[Any, Any, bool]:
async def close(self) -> bool:
"""
close the underlying muxed stream
:return: true if successful

View File

@ -1,8 +1,8 @@
import asyncio
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
Dict,
List,
Sequence,
@ -18,10 +18,10 @@ from libp2p.peer.peerstore import PeerStore
from libp2p.protocol_muxer.multiselect import Multiselect
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
from libp2p.routing.interfaces import IPeerRouting
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from libp2p.transport.upgrader import TransportUpgrader
from libp2p.transport.transport_interface import ITransport
from libp2p.transport.listener_interface import IListener
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from libp2p.stream_muxer.mplex.mplex_stream import MplexStream
@ -29,9 +29,10 @@ from .network_interface import INetwork
from .notifee_interface import INotifee
from .connection.raw_connection import RawConnection
from .stream.net_stream import NetStream
from .stream.net_stream_interface import INetStream
StreamHandlerFn = Callable[[NetStream], Coroutine[Any, Any, None]]
StreamHandlerFn = Callable[[INetStream], Awaitable[None]]
class Swarm(INetwork):
@ -44,7 +45,7 @@ class Swarm(INetwork):
router: IPeerRouting
connections: Dict[ID, IMuxedConn]
listeners: Dict[str, IListener]
stream_handlers: Dict[NetStream, Callable[[NetStream], None]]
stream_handlers: Dict[INetStream, Callable[[INetStream], None]]
multiselect: Multiselect
multiselect_client: MultiselectClient
@ -252,7 +253,7 @@ class Swarm(INetwork):
# TODO: `disconnect`?
GenericProtocolHandlerFn = Callable[[MplexStream], Coroutine[Any, Any, None]]
GenericProtocolHandlerFn = Callable[[MplexStream], Awaitable[None]]
def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn:

View File

@ -39,10 +39,10 @@ class PeerData(IPeerData):
def clear_addrs(self) -> None:
self.addrs = []
def put_metadata(self, key: Any, val: Any) -> None:
def put_metadata(self, key: str, val: Any) -> None:
self.metadata[key] = val
def get_metadata(self, key: Any) -> Any:
def get_metadata(self, key: str) -> Any:
if key in self.metadata:
return self.metadata[key]
raise PeerDataError("key not found")

View File

@ -7,6 +7,8 @@ from typing import (
from multiaddr import Multiaddr
from .peermetadata_interface import IPeerMetadata
class IPeerData(ABC):
@ -47,7 +49,7 @@ class IPeerData(ABC):
"""
@abstractmethod
def put_metadata(self, key: Any, val: Any) -> None:
def put_metadata(self, key: str, val: Any) -> None:
"""
:param key: key in KV pair
:param val: val to associate with key
@ -55,7 +57,7 @@ class IPeerData(ABC):
"""
@abstractmethod
def get_metadata(self, key: Any) -> Any:
def get_metadata(self, key: str) -> IPeerMetadata:
"""
:param key: key in KV pair
:return: val for key

View File

@ -14,7 +14,7 @@ class IPeerMetadata(ABC):
pass
@abstractmethod
def get(self, peer_id: ID, key: Any) -> Any:
def get(self, peer_id: ID, key: str) -> Any:
"""
:param peer_id: peer ID to lookup key for
:param key: key to look up
@ -23,7 +23,7 @@ class IPeerMetadata(ABC):
"""
@abstractmethod
def put(self, peer_id: ID, key: Any, val: Any) -> None:
def put(self, peer_id: ID, key: str, val: Any) -> None:
"""
:param peer_id: peer ID to lookup key for
:param key: key to associate with peer

View File

@ -58,13 +58,13 @@ class PeerStore(IPeerStore):
def peer_ids(self) -> List[ID]:
return list(self.peer_map.keys())
def get(self, peer_id: ID, key: Any) -> Any:
def get(self, peer_id: ID, key: str) -> Any:
if peer_id in self.peer_map:
val = self.peer_map[peer_id].get_metadata(key)
return val
raise PeerStoreError("peer ID not found")
def put(self, peer_id: ID, key: Any, val: Any) -> None:
def put(self, peer_id: ID, key: str, val: Any) -> None:
# <<?>>
# This can output an error, not sure what the possible errors are
peer = self.__create_or_get_peer(peer_id)

View File

@ -306,8 +306,7 @@ class Pubsub:
# Tell router we are leaving this topic
await self.router.leave(topic_id)
# FIXME: `raw_msg` can be further type hinted with mypy_protobuf
async def message_all_peers(self, raw_msg: Any) -> None:
async def message_all_peers(self, raw_msg: bytes) -> None:
"""
Broadcast a message to peers
:param raw_msg: raw contents of the message to broadcast

View File

@ -1,7 +1,6 @@
from abc import ABC, abstractmethod
from typing import (
Any,
Coroutine,
Iterable,
)
@ -31,7 +30,7 @@ class IContentRouting(ABC):
class IPeerRouting(ABC):
@abstractmethod
def find_peer(self, peer_id: ID) -> Coroutine[Any, Any, PeerInfo]:
async def find_peer(self, peer_id: ID) -> PeerInfo:
"""
Find specific Peer
FindPeer searches for a peer with given peer_id, returns a peer.PeerInfo

View File

@ -9,6 +9,7 @@ from libp2p.kademlia.kad_peerinfo import (
)
from libp2p.kademlia.network import KademliaServer
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo
from libp2p.routing.interfaces import IPeerRouting

View File

@ -1,7 +1,8 @@
import asyncio
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
from .utils import get_flag
from ..muxed_stream_interface import IMuxedStream
class MplexStream(IMuxedStream):

View File

@ -1,8 +1,12 @@
from abc import ABC, abstractmethod
from libp2p.stream_muxer.mplex.mplex import Mplex
class IMuxedStream(ABC):
mplex_conn: Mplex
@abstractmethod
def read(self):
"""