From 29091266fce18b6bea7e4b2f4bca10e039a59935 Mon Sep 17 00:00:00 2001 From: Chih Cheng Liang Date: Mon, 5 Aug 2019 10:35:56 +0800 Subject: [PATCH] add still needed TYPE_CHECK --- libp2p/network/swarm.py | 4 +--- libp2p/network/typing.py | 4 ++++ libp2p/stream_muxer/mplex/mplex.py | 6 +++--- libp2p/stream_muxer/muxed_connection_interface.py | 11 ++++++++--- libp2p/stream_muxer/muxed_stream_interface.py | 7 +++++-- libp2p/transport/upgrader.py | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 libp2p/network/typing.py diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index ce80b0e..eccca25 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -19,6 +19,7 @@ from .network_interface import INetwork from .notifee_interface import INotifee from .stream.net_stream import NetStream from .stream.net_stream_interface import INetStream +from .typing import GenericProtocolHandlerFn StreamHandlerFn = Callable[[INetStream], Awaitable[None]] @@ -248,9 +249,6 @@ class Swarm(INetwork): # TODO: `disconnect`? -GenericProtocolHandlerFn = Callable[[IMuxedStream], Awaitable[None]] - - def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn: """ Create a generic protocol handler from the given swarm. We use swarm diff --git a/libp2p/network/typing.py b/libp2p/network/typing.py new file mode 100644 index 0000000..55b577e --- /dev/null +++ b/libp2p/network/typing.py @@ -0,0 +1,4 @@ +from typing import Awaitable, Callable +from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream + +GenericProtocolHandlerFn = Callable[[IMuxedStream], Awaitable[None]] diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index 6e68fdc..c359ee3 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -4,7 +4,7 @@ from typing import Tuple, Dict from multiaddr import Multiaddr from libp2p.security.secure_conn_interface import ISecureConn from libp2p.network.connection.raw_connection_interface import IRawConnection -from libp2p.network.swarm import GenericProtocolHandlerFn +from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream @@ -24,8 +24,8 @@ class Mplex(IMuxedConn): initiator: bool generic_protocol_handler = None peer_id: ID - buffers: Dict[int, asyncio.Queue[bytes]] - stream_queue: asyncio.Queue[int] + buffers: Dict[int, "asyncio.Queue[bytes]"] + stream_queue: "asyncio.Queue[int]" def __init__( self, diff --git a/libp2p/stream_muxer/muxed_connection_interface.py b/libp2p/stream_muxer/muxed_connection_interface.py index dfaa58b..9c54007 100644 --- a/libp2p/stream_muxer/muxed_connection_interface.py +++ b/libp2p/stream_muxer/muxed_connection_interface.py @@ -2,11 +2,16 @@ from abc import ABC, abstractmethod from multiaddr import Multiaddr from libp2p.security.secure_conn_interface import ISecureConn -from libp2p.network.swarm import GenericProtocolHandlerFn +from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream from libp2p.stream_muxer.mplex.constants import HeaderTags +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream + + class IMuxedConn(ABC): """ @@ -55,7 +60,7 @@ class IMuxedConn(ABC): @abstractmethod async def open_stream( self, protocol_id: str, multi_addr: Multiaddr - ) -> IMuxedStream: + ) -> "IMuxedStream": """ creates a new muxed_stream :param protocol_id: protocol_id of stream diff --git a/libp2p/stream_muxer/muxed_stream_interface.py b/libp2p/stream_muxer/muxed_stream_interface.py index a4c602c..141ba9f 100644 --- a/libp2p/stream_muxer/muxed_stream_interface.py +++ b/libp2p/stream_muxer/muxed_stream_interface.py @@ -1,11 +1,14 @@ from abc import ABC, abstractmethod +from typing import TYPE_CHECKING -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn + +if TYPE_CHECKING: + from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn class IMuxedStream(ABC): - mplex_conn: IMuxedConn + mplex_conn: "IMuxedConn" @abstractmethod async def read(self) -> bytes: diff --git a/libp2p/transport/upgrader.py b/libp2p/transport/upgrader.py index 56b4c61..6809fbb 100644 --- a/libp2p/transport/upgrader.py +++ b/libp2p/transport/upgrader.py @@ -3,7 +3,7 @@ from typing import Dict, Sequence from libp2p.stream_muxer.mplex.mplex import Mplex from libp2p.security.security_multistream import SecurityMultistream, TProtocol from libp2p.network.connection.raw_connection_interface import IRawConnection -from libp2p.network.swarm import GenericProtocolHandlerFn +from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID from libp2p.security.secure_conn_interface import ISecureConn from libp2p.security.secure_transport_interface import ISecureTransport