Skip to content

Commit 13d730a

Browse files
committed
fix: improve types according to new typecheck
1 parent 47ae20d commit 13d730a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

examples/doc-examples/example_net_stream.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async def run_enhanced_demo(
193193
print("🖥️ ENHANCED ECHO SERVER MODE")
194194
print("=" * 60)
195195

196-
# Set the enhanced stream handler
196+
# type: ignore: Stream is type of NetStream
197197
host.set_stream_handler(PROTOCOL_ID, enhanced_echo_handler)
198198

199199
print(
@@ -217,7 +217,8 @@ async def run_enhanced_demo(
217217

218218
# Create stream and run enhanced demo
219219
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
220-
await enhanced_client_demo(stream)
220+
if isinstance(stream, NetStream):
221+
await enhanced_client_demo(stream)
221222

222223
print("\n" + "=" * 60)
223224
print("CLIENT DEMO COMPLETE")

libp2p/network/stream/net_stream.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from enum import (
22
Enum,
33
)
4-
from typing import (
5-
Optional,
6-
)
74

85
import trio
96

@@ -102,11 +99,11 @@ class NetStream(INetStream):
10299
"""
103100

104101
muxed_stream: IMuxedStream
105-
protocol_id: Optional[TProtocol]
102+
protocol_id: TProtocol | None
106103
__stream_state: StreamState
107104

108105
def __init__(
109-
self, muxed_stream: IMuxedStream, nursery: Optional[trio.Nursery] = None
106+
self, muxed_stream: IMuxedStream, nursery: trio.Nursery | None = None
110107
) -> None:
111108
super().__init__()
112109

@@ -142,7 +139,7 @@ async def state(self) -> StreamState:
142139
async with self._state_lock:
143140
return self.__stream_state
144141

145-
async def read(self, n: Optional[int] = None) -> bytes:
142+
async def read(self, n: int | None = None) -> bytes:
146143
"""
147144
Read from stream.
148145
@@ -279,7 +276,7 @@ async def _notify_closed(self) -> None:
279276
if hasattr(swarm, "refs") and hasattr(swarm.refs, "done"):
280277
swarm.refs.done()
281278

282-
def get_remote_address(self) -> Optional[tuple[str, int]]:
279+
def get_remote_address(self) -> tuple[str, int] | None:
283280
"""Delegate to the underlying muxed stream."""
284281
return self.muxed_stream.get_remote_address()
285282

0 commit comments

Comments
 (0)