33import asyncio
44import builtins
55from collections import deque
6- from typing import Deque , Final , Optional , Set , Tuple , Type , Union
6+ from typing import Final
77
88from ..base_protocol import BaseProtocol
99from ..compression_utils import ZLibDecompressor
2323 WSMsgType ,
2424)
2525
26- ALLOWED_CLOSE_CODES : Final [Set [int ]] = {int (i ) for i in WSCloseCode }
26+ ALLOWED_CLOSE_CODES : Final [set [int ]] = {int (i ) for i in WSCloseCode }
2727
2828# States for the reader, used to parse the WebSocket frame
2929# integer values are used so they can be cythonized
@@ -70,21 +70,21 @@ def __init__(
7070 self ._limit = limit * 2
7171 self ._loop = loop
7272 self ._eof = False
73- self ._waiter : Optional [ asyncio .Future [None ]] = None
74- self ._exception : Union [ Type [ BaseException ], BaseException , None ] = None
75- self ._buffer : Deque [WSMessage ] = deque ()
73+ self ._waiter : asyncio .Future [None ] | None = None
74+ self ._exception : type [ BaseException ] | BaseException | None = None
75+ self ._buffer : deque [WSMessage ] = deque ()
7676 self ._get_buffer = self ._buffer .popleft
7777 self ._put_buffer = self ._buffer .append
7878
7979 def is_eof (self ) -> bool :
8080 return self ._eof
8181
82- def exception (self ) -> Optional [ Union [ Type [ BaseException ], BaseException ]] :
82+ def exception (self ) -> type [ BaseException ] | BaseException | None :
8383 return self ._exception
8484
8585 def set_exception (
8686 self ,
87- exc : Union [ Type [ BaseException ], BaseException ] ,
87+ exc : type [ BaseException ] | BaseException ,
8888 exc_cause : builtins .BaseException = _EXC_SENTINEL ,
8989 ) -> None :
9090 self ._eof = True
@@ -144,7 +144,7 @@ def __init__(
144144 self .queue = queue
145145 self ._max_msg_size = max_msg_size
146146
147- self ._exc : Optional [ Exception ] = None
147+ self ._exc : Exception | None = None
148148 self ._partial = bytearray ()
149149 self ._state = READ_HEADER
150150
@@ -156,11 +156,11 @@ def __init__(
156156
157157 self ._tail : bytes = b""
158158 self ._has_mask = False
159- self ._frame_mask : Optional [ bytes ] = None
159+ self ._frame_mask : bytes | None = None
160160 self ._payload_bytes_to_read = 0
161161 self ._payload_len_flag = 0
162162 self ._compressed : int = COMPRESSED_NOT_SET
163- self ._decompressobj : Optional [ ZLibDecompressor ] = None
163+ self ._decompressobj : ZLibDecompressor | None = None
164164 self ._compress = compress
165165
166166 def feed_eof (self ) -> None :
@@ -169,9 +169,7 @@ def feed_eof(self) -> None:
169169 # data can be bytearray on Windows because proactor event loop uses bytearray
170170 # and asyncio types this to Union[bytes, bytearray, memoryview] so we need
171171 # coerce data to bytes if it is not
172- def feed_data (
173- self , data : Union [bytes , bytearray , memoryview ]
174- ) -> Tuple [bool , bytes ]:
172+ def feed_data (self , data : bytes | bytearray | memoryview ) -> tuple [bool , bytes ]:
175173 if type (data ) is not bytes :
176174 data = bytes (data )
177175
@@ -190,9 +188,9 @@ def feed_data(
190188 def _handle_frame (
191189 self ,
192190 fin : bool ,
193- opcode : Union [ int , cython_int ] , # Union intended: Cython pxd uses C int
194- payload : Union [ bytes , bytearray ] ,
195- compressed : Union [ int , cython_int ] , # Union intended: Cython pxd uses C int
191+ opcode : int | cython_int , # Union intended: Cython pxd uses C int
192+ payload : bytes | bytearray ,
193+ compressed : int | cython_int , # Union intended: Cython pxd uses C int
196194 ) -> None :
197195 msg : WSMessage
198196 if opcode in {OP_CODE_TEXT , OP_CODE_BINARY , OP_CODE_CONTINUATION }:
@@ -228,7 +226,7 @@ def _handle_frame(
228226 f"to be zero, got { opcode !r} " ,
229227 )
230228
231- assembled_payload : Union [ bytes , bytearray ]
229+ assembled_payload : bytes | bytearray
232230 if has_partial :
233231 assembled_payload = self ._partial + payload
234232 self ._partial .clear ()
@@ -452,7 +450,7 @@ def _feed_data(self, data: bytes) -> None:
452450 self ._payload_fragments .append (data_cstr [f_start_pos :f_end_pos ])
453451 break
454452
455- payload : Union [ bytes , bytearray ]
453+ payload : bytes | bytearray
456454 if had_fragments :
457455 # We have to join the payload fragments get the payload
458456 self ._payload_fragments .append (data_cstr [f_start_pos :f_end_pos ])
0 commit comments