Skip to content

Commit

Permalink
Fix some type hinting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Oct 17, 2021
1 parent 5f68e08 commit 11cfdca
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions sanic/server/protocols/websocket_protocol.py
@@ -1,7 +1,8 @@
from typing import TYPE_CHECKING, Optional, Sequence
from typing import TYPE_CHECKING, Optional, Sequence, cast

from websockets.connection import CLOSED, CLOSING, OPEN
from websockets.server import ServerConnection
from websockets.typing import Subprotocol

from sanic.exceptions import ServerError
from sanic.log import error_logger
Expand All @@ -15,13 +16,6 @@


class WebSocketProtocol(HttpProtocol):

websocket: Optional[WebsocketImplProtocol]
websocket_timeout: float
websocket_max_size = Optional[int]
websocket_ping_interval = Optional[float]
websocket_ping_timeout = Optional[float]

def __init__(
self,
*args,
Expand All @@ -35,7 +29,7 @@ def __init__(
**kwargs,
):
super().__init__(*args, **kwargs)
self.websocket = None
self.websocket: Optional[WebsocketImplProtocol] = None
self.websocket_timeout = websocket_timeout
self.websocket_max_size = websocket_max_size
if websocket_max_queue is not None and websocket_max_queue > 0:
Expand Down Expand Up @@ -116,7 +110,15 @@ async def websocket_handshake(
if subprotocols is not None:
# subprotocols can be a set or frozenset,
# but ServerConnection needs a list
subprotocols = list(subprotocols)
subprotocols = cast(
Optional[Sequence[Subprotocol]],
list(
[
Subprotocol(subprotocol)
for subprotocol in subprotocols
]
),
)
ws_conn = ServerConnection(
max_size=self.websocket_max_size,
subprotocols=subprotocols,
Expand All @@ -142,7 +144,7 @@ async def websocket_handshake(
)
rbody += "".join(f"{k}: {v}\r\n" for k, v in resp.headers.items())
if resp.body is not None:
rbody += f"\r\n{resp.body}\r\n\r\n"
rbody += f"\r\n{resp.body.decode('utf-8')}\r\n\r\n"
else:
rbody += "\r\n"
await super().send(rbody.encode())
Expand Down

0 comments on commit 11cfdca

Please sign in to comment.