3939 Opcode ,
4040)
4141from ..protocol import State
42- from ..typing import Data , LoggerLike , Subprotocol
42+ from ..typing import BytesLike , Data , DataLike , LoggerLike , Subprotocol
4343from .framing import Frame , prepare_ctrl , prepare_data
4444
4545
@@ -563,7 +563,7 @@ async def recv(self) -> Data:
563563
564564 async def send (
565565 self ,
566- message : Data | Iterable [Data ] | AsyncIterable [Data ],
566+ message : DataLike | Iterable [DataLike ] | AsyncIterable [DataLike ],
567567 ) -> None :
568568 """
569569 Send a message.
@@ -638,7 +638,7 @@ async def send(
638638
639639 elif isinstance (message , Iterable ):
640640 # Work around https://github.com/python/mypy/issues/6227
641- message = cast (Iterable [Data ], message )
641+ message = cast (Iterable [DataLike ], message )
642642
643643 iter_message = iter (message )
644644 try :
@@ -678,14 +678,14 @@ async def send(
678678 # Implement aiter_message = aiter(message) without aiter
679679 # Work around https://github.com/python/mypy/issues/5738
680680 aiter_message = cast (
681- Callable [[AsyncIterable [Data ]], AsyncIterator [Data ]],
681+ Callable [[AsyncIterable [DataLike ]], AsyncIterator [DataLike ]],
682682 type (message ).__aiter__ ,
683683 )(message )
684684 try :
685685 # Implement fragment = anext(aiter_message) without anext
686686 # Work around https://github.com/python/mypy/issues/5738
687687 fragment = await cast (
688- Callable [[AsyncIterator [Data ]], Awaitable [Data ]],
688+ Callable [[AsyncIterator [DataLike ]], Awaitable [DataLike ]],
689689 type (aiter_message ).__anext__ ,
690690 )(aiter_message )
691691 except StopAsyncIteration :
@@ -788,7 +788,7 @@ async def wait_closed(self) -> None:
788788 """
789789 await asyncio .shield (self .connection_lost_waiter )
790790
791- async def ping (self , data : Data | None = None ) -> Awaitable [float ]:
791+ async def ping (self , data : DataLike | None = None ) -> Awaitable [float ]:
792792 """
793793 Send a Ping_.
794794
@@ -847,7 +847,7 @@ async def ping(self, data: Data | None = None) -> Awaitable[float]:
847847
848848 return asyncio .shield (pong_waiter )
849849
850- async def pong (self , data : Data = b"" ) -> None :
850+ async def pong (self , data : DataLike = b"" ) -> None :
851851 """
852852 Send a Pong_.
853853
@@ -1025,10 +1025,12 @@ async def read_message(self) -> Data | None:
10251025
10261026 # Shortcut for the common case - no fragmentation
10271027 if frame .fin :
1028+ if isinstance (frame .data , memoryview ):
1029+ raise AssertionError ("only compressed outgoing frames use memoryview" )
10281030 return frame .data .decode () if text else bytes (frame .data )
10291031
10301032 # 5.4. Fragmentation
1031- fragments : list [Data ] = []
1033+ fragments : list [DataLike ] = []
10321034 max_size = self .max_size
10331035 if text :
10341036 decoder_factory = codecs .getincrementaldecoder ("utf-8" )
@@ -1152,7 +1154,7 @@ async def read_frame(self, max_size: int | None) -> Frame:
11521154 self .logger .debug ("< %s" , frame )
11531155 return frame
11541156
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 :
11561158 frame = Frame (fin , Opcode (opcode ), data )
11571159 if self .debug :
11581160 self .logger .debug ("> %s" , frame )
@@ -1174,7 +1176,7 @@ async def drain(self) -> None:
11741176 await self .ensure_open ()
11751177
11761178 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
11781180 ) -> None :
11791181 # Defensive assertion for protocol compliance.
11801182 if self .state is not _state : # pragma: no cover
@@ -1184,7 +1186,9 @@ async def write_frame(
11841186 self .write_frame_sync (fin , opcode , data )
11851187 await self .drain ()
11861188
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 :
11881192 """
11891193 Write a close frame if and only if the connection state is OPEN.
11901194
@@ -1538,7 +1542,7 @@ def eof_received(self) -> None:
15381542
15391543def broadcast (
15401544 websockets : Iterable [WebSocketCommonProtocol ],
1541- message : Data ,
1545+ message : DataLike ,
15421546 raise_exceptions : bool = False ,
15431547) -> None :
15441548 """
0 commit comments