From 87ef2e4618f551faf1fb42ec055cb8309431441b Mon Sep 17 00:00:00 2001 From: Chih Cheng Liang Date: Mon, 5 Aug 2019 10:20:30 +0800 Subject: [PATCH] remove if TYPE_CHECKING as much as possible --- libp2p/stream_muxer/mplex/mplex.py | 33 +++++++-------- libp2p/stream_muxer/mplex/mplex_stream.py | 13 ++---- .../muxed_connection_interface.py | 27 ++++++------ libp2p/transport/listener_interface.py | 11 ++--- libp2p/transport/tcp/tcp.py | 19 ++++----- libp2p/transport/transport_interface.py | 17 ++++---- libp2p/transport/upgrader.py | 42 +++++++++---------- 7 files changed, 69 insertions(+), 93 deletions(-) diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index fe414c7..6e68fdc 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -1,40 +1,37 @@ import asyncio -from typing import TYPE_CHECKING, Tuple, Dict +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.peer.id import ID from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream from .constants import HeaderTags from .utils import encode_uvarint, decode_uvarint_from_stream from .mplex_stream import MplexStream -if TYPE_CHECKING: - 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.peer.id import ID - from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream - - class Mplex(IMuxedConn): """ reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go """ - secured_conn: "ISecureConn" - raw_conn: "IRawConnection" + secured_conn: ISecureConn + raw_conn: IRawConnection initiator: bool generic_protocol_handler = None - peer_id: "ID" + peer_id: ID buffers: Dict[int, asyncio.Queue[bytes]] stream_queue: asyncio.Queue[int] def __init__( self, - secured_conn: "ISecureConn", - generic_protocol_handler: "GenericProtocolHandlerFn", - peer_id: "ID", + secured_conn: ISecureConn, + generic_protocol_handler: GenericProtocolHandlerFn, + peer_id: ID, ) -> None: """ create a new muxed connection @@ -95,8 +92,8 @@ class Mplex(IMuxedConn): return None async def open_stream( - self, protocol_id: str, multi_addr: "Multiaddr" - ) -> "IMuxedStream": + self, protocol_id: str, multi_addr: Multiaddr + ) -> IMuxedStream: """ creates a new muxed_stream :param protocol_id: protocol_id of stream diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index 5e64ac7..d90a593 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -1,14 +1,11 @@ import asyncio -from typing import TYPE_CHECKING from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream +from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn + from .constants import HeaderTags -if TYPE_CHECKING: - from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn - - class MplexStream(IMuxedStream): """ reference: https://github.com/libp2p/go-mplex/blob/master/stream.go @@ -16,16 +13,14 @@ class MplexStream(IMuxedStream): stream_id: int initiator: bool - mplex_conn: "IMuxedConn" + mplex_conn: IMuxedConn read_deadline: float write_deadline: float local_closed: bool remote_closed: bool stream_lock: asyncio.Lock - def __init__( - self, stream_id: int, initiator: bool, mplex_conn: "IMuxedConn" - ) -> None: + def __init__(self, stream_id: int, initiator: bool, mplex_conn: IMuxedConn) -> None: """ create new MuxedStream in muxer :param stream_id: stream stream id diff --git a/libp2p/stream_muxer/muxed_connection_interface.py b/libp2p/stream_muxer/muxed_connection_interface.py index ca23d9d..dfaa58b 100644 --- a/libp2p/stream_muxer/muxed_connection_interface.py +++ b/libp2p/stream_muxer/muxed_connection_interface.py @@ -1,14 +1,11 @@ from abc import ABC, abstractmethod -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from multiaddr import Multiaddr - from libp2p.security.secure_conn_interface import ISecureConn - from libp2p.network.swarm 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 multiaddr import Multiaddr +from libp2p.security.secure_conn_interface import ISecureConn +from libp2p.network.swarm 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 class IMuxedConn(ABC): @@ -17,14 +14,14 @@ class IMuxedConn(ABC): """ initiator: bool - peer_id: "ID" + peer_id: ID @abstractmethod def __init__( self, - conn: "ISecureConn", - generic_protocol_handler: "GenericProtocolHandlerFn", - peer_id: "ID", + conn: ISecureConn, + generic_protocol_handler: GenericProtocolHandlerFn, + peer_id: ID, ) -> None: """ create a new muxed connection @@ -57,8 +54,8 @@ class IMuxedConn(ABC): @abstractmethod async def open_stream( - self, protocol_id: str, multi_addr: "Multiaddr" - ) -> "IMuxedStream": + self, protocol_id: str, multi_addr: Multiaddr + ) -> IMuxedStream: """ creates a new muxed_stream :param protocol_id: protocol_id of stream diff --git a/libp2p/transport/listener_interface.py b/libp2p/transport/listener_interface.py index 8e2d86b..051d289 100644 --- a/libp2p/transport/listener_interface.py +++ b/libp2p/transport/listener_interface.py @@ -1,15 +1,12 @@ from abc import ABC, abstractmethod -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from typing import List - from multiaddr import Multiaddr +from typing import List +from multiaddr import Multiaddr class IListener(ABC): @abstractmethod - async def listen(self, maddr: "Multiaddr") -> bool: + async def listen(self, maddr: Multiaddr) -> bool: """ put listener in listening mode and wait for incoming connections :param maddr: multiaddr of peer @@ -17,7 +14,7 @@ class IListener(ABC): """ @abstractmethod - def get_addrs(self) -> "List[Multiaddr]": + def get_addrs(self) -> List[Multiaddr]: """ retrieve list of addresses the listener is listening on :return: return list of addrs diff --git a/libp2p/transport/tcp/tcp.py b/libp2p/transport/tcp/tcp.py index 918f712..b3d2fc8 100644 --- a/libp2p/transport/tcp/tcp.py +++ b/libp2p/transport/tcp/tcp.py @@ -1,26 +1,23 @@ import asyncio +from typing import List, Callable from multiaddr import Multiaddr +from socket import socket +from libp2p.transport.typing import THandler +from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.network.connection.raw_connection import RawConnection from libp2p.transport.listener_interface import IListener from libp2p.transport.transport_interface import ITransport -from typing import TYPE_CHECKING, List, Callable - -if TYPE_CHECKING: - from socket import socket - from libp2p.transport.typing import THandler - from libp2p.network.connection.raw_connection_interface import IRawConnection - class TCPListener(IListener): multiaddrs: List[Multiaddr] server = None handler = None - def __init__(self, handler_function: "THandler" = None) -> None: + def __init__(self, handler_function: THandler = None) -> None: self.multiaddrs = [] self.server = None self.handler = handler_function @@ -69,7 +66,7 @@ class TCP(ITransport): def __init__(self) -> None: self.listener = TCPListener() - async def dial(self, maddr: Multiaddr, self_id: ID) -> "IRawConnection": + async def dial(self, maddr: Multiaddr, self_id: ID) -> IRawConnection: """ dial a transport to peer listening on multiaddr :param maddr: multiaddr of peer @@ -94,7 +91,7 @@ class TCP(ITransport): return RawConnection(host, str(port), reader, writer, True) - def create_listener(self, handler_function: "THandler") -> TCPListener: + def create_listener(self, handler_function: THandler) -> TCPListener: """ create listener on transport :param handler_function: a function called when a new connection is received @@ -104,5 +101,5 @@ class TCP(ITransport): return TCPListener(handler_function) -def _multiaddr_from_socket(socket: "socket") -> Multiaddr: +def _multiaddr_from_socket(socket: socket) -> Multiaddr: return Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname()) diff --git a/libp2p/transport/transport_interface.py b/libp2p/transport/transport_interface.py index b79d246..38ea365 100644 --- a/libp2p/transport/transport_interface.py +++ b/libp2p/transport/transport_interface.py @@ -1,18 +1,15 @@ from abc import ABC, abstractmethod -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from multiaddr import Multiaddr - from libp2p.peer.id import ID - from libp2p.network.connection.raw_connection_interface import IRawConnection - from .listener_interface import IListener - from .typing import THandler +from multiaddr import Multiaddr +from libp2p.peer.id import ID +from libp2p.network.connection.raw_connection_interface import IRawConnection +from .listener_interface import IListener +from .typing import THandler class ITransport(ABC): @abstractmethod - async def dial(self, maddr: "Multiaddr", self_id: "ID") -> "IRawConnection": + async def dial(self, maddr: Multiaddr, self_id: ID) -> IRawConnection: """ dial a transport to peer listening on multiaddr :param multiaddr: multiaddr of peer @@ -21,7 +18,7 @@ class ITransport(ABC): """ @abstractmethod - def create_listener(self, handler_function: "THandler") -> "IListener": + def create_listener(self, handler_function: THandler) -> IListener: """ create listener on transport :param handler_function: a function called when a new conntion is received diff --git a/libp2p/transport/upgrader.py b/libp2p/transport/upgrader.py index 3cbdad4..56b4c61 100644 --- a/libp2p/transport/upgrader.py +++ b/libp2p/transport/upgrader.py @@ -1,27 +1,23 @@ -from libp2p.security.security_multistream import SecurityMultistream +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.peer.id import ID +from libp2p.security.secure_conn_interface import ISecureConn +from libp2p.security.secure_transport_interface import ISecureTransport -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - - from typing import Dict, Sequence - from libp2p.network.connection.raw_connection_interface import IRawConnection - from libp2p.network.swarm import GenericProtocolHandlerFn - from libp2p.peer.id import ID - from libp2p.security.secure_conn_interface import ISecureConn - from libp2p.security.secure_transport_interface import ISecureTransport - from libp2p.security.security_multistream import TProtocol - from .transport_interface import ITransport - from .listener_interface import IListener +from .transport_interface import ITransport +from .listener_interface import IListener class TransportUpgrader: security_multistream: SecurityMultistream - muxer: "Sequence[str]" + muxer: Sequence[str] def __init__( - self, secOpt: "Dict[TProtocol, ISecureTransport]", muxerOpt: "Sequence[str]" + self, secOpt: Dict[TProtocol, ISecureTransport], muxerOpt: Sequence[str] ) -> None: # Store security option self.security_multistream = SecurityMultistream() @@ -31,15 +27,15 @@ class TransportUpgrader: # Store muxer option self.muxer = muxerOpt - def upgrade_listener(self, transport: "ITransport", listeners: "IListener") -> None: + def upgrade_listener(self, transport: ITransport, listeners: IListener) -> None: """ Upgrade multiaddr listeners to libp2p-transport listeners """ pass async def upgrade_security( - self, raw_conn: "IRawConnection", peer_id: "ID", initiator: bool - ) -> "ISecureConn": + self, raw_conn: IRawConnection, peer_id: ID, initiator: bool + ) -> ISecureConn: """ Upgrade conn to be a secured connection """ @@ -50,10 +46,10 @@ class TransportUpgrader: @staticmethod def upgrade_connection( - conn: "IRawConnection", - generic_protocol_handler: "GenericProtocolHandlerFn", - peer_id: "ID", - ) -> "Mplex": + conn: IRawConnection, + generic_protocol_handler: GenericProtocolHandlerFn, + peer_id: ID, + ) -> Mplex: """ Upgrade raw connection to muxed connection """