39
39
Opcode ,
40
40
)
41
41
from ..protocol import State
42
- from ..typing import Data , LoggerLike , Subprotocol
42
+ from ..typing import BytesLike , Data , DataLike , LoggerLike , Subprotocol
43
43
from .framing import Frame , prepare_ctrl , prepare_data
44
44
45
45
@@ -563,7 +563,7 @@ async def recv(self) -> Data:
563
563
564
564
async def send (
565
565
self ,
566
- message : Data | Iterable [Data ] | AsyncIterable [Data ],
566
+ message : DataLike | Iterable [DataLike ] | AsyncIterable [DataLike ],
567
567
) -> None :
568
568
"""
569
569
Send a message.
@@ -638,7 +638,7 @@ async def send(
638
638
639
639
elif isinstance (message , Iterable ):
640
640
# Work around https://github.com/python/mypy/issues/6227
641
- message = cast (Iterable [Data ], message )
641
+ message = cast (Iterable [DataLike ], message )
642
642
643
643
iter_message = iter (message )
644
644
try :
@@ -678,14 +678,14 @@ async def send(
678
678
# Implement aiter_message = aiter(message) without aiter
679
679
# Work around https://github.com/python/mypy/issues/5738
680
680
aiter_message = cast (
681
- Callable [[AsyncIterable [Data ]], AsyncIterator [Data ]],
681
+ Callable [[AsyncIterable [DataLike ]], AsyncIterator [DataLike ]],
682
682
type (message ).__aiter__ ,
683
683
)(message )
684
684
try :
685
685
# Implement fragment = anext(aiter_message) without anext
686
686
# Work around https://github.com/python/mypy/issues/5738
687
687
fragment = await cast (
688
- Callable [[AsyncIterator [Data ]], Awaitable [Data ]],
688
+ Callable [[AsyncIterator [DataLike ]], Awaitable [DataLike ]],
689
689
type (aiter_message ).__anext__ ,
690
690
)(aiter_message )
691
691
except StopAsyncIteration :
@@ -788,7 +788,7 @@ async def wait_closed(self) -> None:
788
788
"""
789
789
await asyncio .shield (self .connection_lost_waiter )
790
790
791
- async def ping (self , data : Data | None = None ) -> Awaitable [float ]:
791
+ async def ping (self , data : DataLike | None = None ) -> Awaitable [float ]:
792
792
"""
793
793
Send a Ping_.
794
794
@@ -847,7 +847,7 @@ async def ping(self, data: Data | None = None) -> Awaitable[float]:
847
847
848
848
return asyncio .shield (pong_waiter )
849
849
850
- async def pong (self , data : Data = b"" ) -> None :
850
+ async def pong (self , data : DataLike = b"" ) -> None :
851
851
"""
852
852
Send a Pong_.
853
853
@@ -1025,10 +1025,12 @@ async def read_message(self) -> Data | None:
1025
1025
1026
1026
# Shortcut for the common case - no fragmentation
1027
1027
if frame .fin :
1028
+ if isinstance (frame .data , memoryview ):
1029
+ raise AssertionError ("only compressed outgoing frames use memoryview" )
1028
1030
return frame .data .decode () if text else bytes (frame .data )
1029
1031
1030
1032
# 5.4. Fragmentation
1031
- fragments : list [Data ] = []
1033
+ fragments : list [DataLike ] = []
1032
1034
max_size = self .max_size
1033
1035
if text :
1034
1036
decoder_factory = codecs .getincrementaldecoder ("utf-8" )
@@ -1152,7 +1154,7 @@ async def read_frame(self, max_size: int | None) -> Frame:
1152
1154
self .logger .debug ("< %s" , frame )
1153
1155
return frame
1154
1156
1155
- def write_frame_sync (self , fin : bool , opcode : int , data : bytes ) -> None :
1157
+ def write_frame_sync (self , fin : bool , opcode : int , data : BytesLike ) -> None :
1156
1158
frame = Frame (fin , Opcode (opcode ), data )
1157
1159
if self .debug :
1158
1160
self .logger .debug ("> %s" , frame )
@@ -1174,7 +1176,7 @@ async def drain(self) -> None:
1174
1176
await self .ensure_open ()
1175
1177
1176
1178
async def write_frame (
1177
- self , fin : bool , opcode : int , data : bytes , * , _state : int = State .OPEN
1179
+ self , fin : bool , opcode : int , data : BytesLike , * , _state : int = State .OPEN
1178
1180
) -> None :
1179
1181
# Defensive assertion for protocol compliance.
1180
1182
if self .state is not _state : # pragma: no cover
@@ -1184,7 +1186,9 @@ async def write_frame(
1184
1186
self .write_frame_sync (fin , opcode , data )
1185
1187
await self .drain ()
1186
1188
1187
- async def write_close_frame (self , close : Close , data : bytes | None = None ) -> None :
1189
+ async def write_close_frame (
1190
+ self , close : Close , data : BytesLike | None = None
1191
+ ) -> None :
1188
1192
"""
1189
1193
Write a close frame if and only if the connection state is OPEN.
1190
1194
@@ -1538,7 +1542,7 @@ def eof_received(self) -> None:
1538
1542
1539
1543
def broadcast (
1540
1544
websockets : Iterable [WebSocketCommonProtocol ],
1541
- message : Data ,
1545
+ message : DataLike ,
1542
1546
raise_exceptions : bool = False ,
1543
1547
) -> None :
1544
1548
"""
0 commit comments