11# This contains the main Connection class. Everything in h11 revolves around
22# this.
3- from typing import (
4- Any ,
5- Callable ,
6- cast ,
7- Dict ,
8- List ,
9- Optional ,
10- overload ,
11- Tuple ,
12- Type ,
13- Union ,
14- )
3+ from typing import Any , Callable , cast , Optional , overload , Union
154
165from ._events import (
176 ConnectionClosed ,
@@ -92,7 +81,7 @@ def _keep_alive(event: Union[Request, Response]) -> bool:
9281
9382def _body_framing (
9483 request_method : bytes , event : Union [Request , Response ]
95- ) -> Tuple [str , Union [Tuple [()], Tuple [int ]]]:
84+ ) -> tuple [str , Union [tuple [()], tuple [int ]]]:
9685 # Called when we enter SEND_BODY to figure out framing information for
9786 # this body.
9887 #
@@ -166,15 +155,15 @@ class Connection:
166155
167156 def __init__ (
168157 self ,
169- our_role : Type [Sentinel ],
158+ our_role : type [Sentinel ],
170159 max_incomplete_event_size : int = DEFAULT_MAX_INCOMPLETE_EVENT_SIZE ,
171160 ) -> None :
172161 self ._max_incomplete_event_size = max_incomplete_event_size
173162 # State and role tracking
174163 if our_role not in (CLIENT , SERVER ):
175164 raise ValueError (f"expected CLIENT or SERVER, not { our_role !r} " )
176165 self .our_role = our_role
177- self .their_role : Type [Sentinel ]
166+ self .their_role : type [Sentinel ]
178167 if our_role is CLIENT :
179168 self .their_role = SERVER
180169 else :
@@ -204,7 +193,7 @@ def __init__(
204193 self .client_is_waiting_for_100_continue = False
205194
206195 @property
207- def states (self ) -> Dict [ Type [Sentinel ], Type [Sentinel ]]:
196+ def states (self ) -> dict [ type [Sentinel ], type [Sentinel ]]:
208197 """A dictionary like::
209198
210199 {CLIENT: <client state>, SERVER: <server state>}
@@ -215,14 +204,14 @@ def states(self) -> Dict[Type[Sentinel], Type[Sentinel]]:
215204 return dict (self ._cstate .states )
216205
217206 @property
218- def our_state (self ) -> Type [Sentinel ]:
207+ def our_state (self ) -> type [Sentinel ]:
219208 """The current state of whichever role we are playing. See
220209 :ref:`state-machine` for details.
221210 """
222211 return self ._cstate .states [self .our_role ]
223212
224213 @property
225- def their_state (self ) -> Type [Sentinel ]:
214+ def their_state (self ) -> type [Sentinel ]:
226215 """The current state of whichever role we are NOT playing. See
227216 :ref:`state-machine` for details.
228217 """
@@ -252,12 +241,12 @@ def start_next_cycle(self) -> None:
252241 assert not self .client_is_waiting_for_100_continue
253242 self ._respond_to_state_changes (old_states )
254243
255- def _process_error (self , role : Type [Sentinel ]) -> None :
244+ def _process_error (self , role : type [Sentinel ]) -> None :
256245 old_states = dict (self ._cstate .states )
257246 self ._cstate .process_error (role )
258247 self ._respond_to_state_changes (old_states )
259248
260- def _server_switch_event (self , event : Event ) -> Optional [Type [Sentinel ]]:
249+ def _server_switch_event (self , event : Event ) -> Optional [type [Sentinel ]]:
261250 if type (event ) is InformationalResponse and event .status_code == 101 :
262251 return _SWITCH_UPGRADE
263252 if type (event ) is Response :
@@ -269,7 +258,7 @@ def _server_switch_event(self, event: Event) -> Optional[Type[Sentinel]]:
269258 return None
270259
271260 # All events go through here
272- def _process_event (self , role : Type [Sentinel ], event : Event ) -> None :
261+ def _process_event (self , role : type [Sentinel ], event : Event ) -> None :
273262 # First, pass the event through the state machine to make sure it
274263 # succeeds.
275264 old_states = dict (self ._cstate .states )
@@ -319,7 +308,7 @@ def _process_event(self, role: Type[Sentinel], event: Event) -> None:
319308
320309 def _get_io_object (
321310 self ,
322- role : Type [Sentinel ],
311+ role : type [Sentinel ],
323312 event : Optional [Event ],
324313 io_dict : Union [ReadersType , WritersType ],
325314 ) -> Optional [Callable [..., Any ]]:
@@ -341,7 +330,7 @@ def _get_io_object(
341330 # self._cstate.states to change.
342331 def _respond_to_state_changes (
343332 self ,
344- old_states : Dict [ Type [Sentinel ], Type [Sentinel ]],
333+ old_states : dict [ type [Sentinel ], type [Sentinel ]],
345334 event : Optional [Event ] = None ,
346335 ) -> None :
347336 # Update reader/writer
@@ -351,7 +340,7 @@ def _respond_to_state_changes(
351340 self ._reader = self ._get_io_object (self .their_role , event , READERS )
352341
353342 @property
354- def trailing_data (self ) -> Tuple [bytes , bool ]:
343+ def trailing_data (self ) -> tuple [bytes , bool ]:
355344 """Data that has been received, but not yet processed, represented as
356345 a tuple with two elements, where the first is a byte-string containing
357346 the unprocessed data itself, and the second is a bool that is True if
@@ -409,7 +398,7 @@ def receive_data(self, data: bytes) -> None:
409398
410399 def _extract_next_receive_event (
411400 self ,
412- ) -> Union [Event , Type [NEED_DATA ], Type [PAUSED ]]:
401+ ) -> Union [Event , type [NEED_DATA ], type [PAUSED ]]:
413402 state = self .their_state
414403 # We don't pause immediately when they enter DONE, because even in
415404 # DONE state we can still process a ConnectionClosed() event. But
@@ -435,7 +424,7 @@ def _extract_next_receive_event(
435424 event = NEED_DATA
436425 return event # type: ignore[no-any-return]
437426
438- def next_event (self ) -> Union [Event , Type [NEED_DATA ], Type [PAUSED ]]:
427+ def next_event (self ) -> Union [Event , type [NEED_DATA ], type [PAUSED ]]:
439428 """Parse the next event out of our receive buffer, update our internal
440429 state, and return it.
441430
@@ -541,7 +530,7 @@ def send(self, event: Event) -> Optional[bytes]:
541530 else :
542531 return b"" .join (data_list )
543532
544- def send_with_data_passthrough (self , event : Event ) -> Optional [List [bytes ]]:
533+ def send_with_data_passthrough (self , event : Event ) -> Optional [list [bytes ]]:
545534 """Identical to :meth:`send`, except that in situations where
546535 :meth:`send` returns a single :term:`bytes-like object`, this instead
547536 returns a list of them -- and when sending a :class:`Data` event, this
@@ -567,7 +556,7 @@ def send_with_data_passthrough(self, event: Event) -> Optional[List[bytes]]:
567556 # In any situation where writer is None, process_event should
568557 # have raised ProtocolError
569558 assert writer is not None
570- data_list : List [bytes ] = []
559+ data_list : list [bytes ] = []
571560 writer (event , data_list .append )
572561 return data_list
573562 except :
0 commit comments