44
55An implementation of a WebSocket connection.
66"""
7+ from __future__ import annotations
78
89from collections import deque
910from enum import Enum
10- from typing import Deque , Generator , List , Optional
11+ from typing import TYPE_CHECKING
1112
1213from .events import (
1314 BytesMessage ,
1819 Pong ,
1920 TextMessage ,
2021)
21- from .extensions import Extension
2222from .frame_protocol import CloseReason , FrameProtocol , Opcode , ParseFailed
2323from .utilities import LocalProtocolError
2424
25+ if TYPE_CHECKING :
26+ from collections .abc import Generator
27+
28+ from .extensions import Extension
29+
2530
2631class ConnectionState (Enum ):
2732 """
@@ -68,7 +73,7 @@ class Connection:
6873 def __init__ (
6974 self ,
7075 connection_type : ConnectionType ,
71- extensions : Optional [ List [ Extension ]] = None ,
76+ extensions : list [ Extension ] | None = None ,
7277 trailing_data : bytes = b"" ,
7378 ) -> None :
7479 """
@@ -82,7 +87,7 @@ def __init__(
8287 processed.
8388 """
8489 self .client = connection_type is ConnectionType .CLIENT
85- self ._events : Deque [Event ] = deque ()
90+ self ._events : deque [Event ] = deque ()
8691 self ._proto = FrameProtocol (self .client , extensions or [])
8792 self ._state = ConnectionState .OPEN
8893 self .receive_data (trailing_data )
@@ -109,12 +114,13 @@ def send(self, event: Event) -> bytes:
109114 else :
110115 self ._state = ConnectionState .LOCAL_CLOSING
111116 else :
117+ msg = f"Event { event } cannot be sent in state { self .state } ."
112118 raise LocalProtocolError (
113- f"Event { event } cannot be sent in state { self . state } ."
119+ msg ,
114120 )
115121 return data
116122
117- def receive_data (self , data : Optional [ bytes ] ) -> None :
123+ def receive_data (self , data : bytes | None ) -> None :
118124 """
119125 Pass some received data to the connection for handling.
120126
@@ -124,7 +130,6 @@ def receive_data(self, data: Optional[bytes]) -> None:
124130 :param data: The data received from the remote peer on the network.
125131 :type data: ``bytes``
126132 """
127-
128133 if data is None :
129134 # "If _The WebSocket Connection is Closed_ and no Close control
130135 # frame was received by the endpoint (such as could occur if the
@@ -137,7 +142,8 @@ def receive_data(self, data: Optional[bytes]) -> None:
137142 if self .state in (ConnectionState .OPEN , ConnectionState .LOCAL_CLOSING ):
138143 self ._proto .receive_bytes (data )
139144 elif self .state is ConnectionState .CLOSED :
140- raise LocalProtocolError ("Connection already closed." )
145+ msg = "Connection already closed."
146+ raise LocalProtocolError (msg )
141147 else :
142148 pass # pragma: no cover
143149
@@ -154,12 +160,14 @@ def events(self) -> Generator[Event, None, None]:
154160 try :
155161 for frame in self ._proto .received_frames ():
156162 if frame .opcode is Opcode .PING :
157- assert frame .frame_finished and frame .message_finished
163+ assert frame .frame_finished
164+ assert frame .message_finished
158165 assert isinstance (frame .payload , (bytes , bytearray ))
159166 yield Ping (payload = frame .payload )
160167
161168 elif frame .opcode is Opcode .PONG :
162- assert frame .frame_finished and frame .message_finished
169+ assert frame .frame_finished
170+ assert frame .message_finished
163171 assert isinstance (frame .payload , (bytes , bytearray ))
164172 yield Pong (payload = frame .payload )
165173
0 commit comments