Skip to content

Commit e378942

Browse files
committed
fix: correct type signature of BasicHost.run method
1 parent b1c996f commit e378942

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

libp2p/abc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
KeysView,
99
Sequence,
1010
)
11+
from contextlib import AbstractAsyncContextManager
1112
from types import (
1213
TracebackType,
1314
)
@@ -1178,7 +1179,9 @@ def get_live_peers(self) -> list[ID]:
11781179
"""
11791180

11801181
@abstractmethod
1181-
def run(self, listen_addrs: Sequence[Multiaddr]) -> AsyncContextManager[None]:
1182+
def run(
1183+
self, listen_addrs: Sequence[Multiaddr]
1184+
) -> AbstractAsyncContextManager[None]:
11821185
"""
11831186
Run the host and start listening on the specified multiaddresses.
11841187

libp2p/host/basic_host.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Sequence,
44
)
55
from contextlib import (
6+
AbstractAsyncContextManager,
67
asynccontextmanager,
78
)
89
import logging
@@ -147,19 +148,23 @@ def get_connected_peers(self) -> list[ID]:
147148
"""
148149
return list(self._network.connections.keys())
149150

150-
@asynccontextmanager
151-
async def run(
151+
def run(
152152
self, listen_addrs: Sequence[multiaddr.Multiaddr]
153-
) -> AsyncIterator[None]:
153+
) -> AbstractAsyncContextManager[None]:
154154
"""
155155
Run the host instance and listen to ``listen_addrs``.
156156
157157
:param listen_addrs: a sequence of multiaddrs that we want to listen to
158158
"""
159-
network = self.get_network()
160-
async with background_trio_service(network):
161-
await network.listen(*listen_addrs)
162-
yield
159+
160+
@asynccontextmanager
161+
async def _run() -> AsyncIterator[None]:
162+
network = self.get_network()
163+
async with background_trio_service(network):
164+
await network.listen(*listen_addrs)
165+
yield
166+
167+
return _run()
163168

164169
def set_stream_handler(
165170
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn

0 commit comments

Comments
 (0)