Skip to content

Commit

Permalink
Make it explicit that status codes can be int.
Browse files Browse the repository at this point in the history
The code and tests already support it but the types didn't reflect it.

Refs #1406.
  • Loading branch information
aaugustin committed Oct 21, 2023
1 parent f62d44a commit ed6cb1b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/reference/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Types

.. autodata:: LoggerLike

.. autodata:: StatusLike

.. autodata:: Origin

.. autodata:: Subprotocol
Expand Down
3 changes: 3 additions & 0 deletions src/websockets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"ExtensionName",
"ExtensionParameter",
"LoggerLike",
"StatusLike",
"Origin",
"Subprotocol",
]
Expand Down Expand Up @@ -115,6 +116,7 @@
ExtensionParameter,
LoggerLike,
Origin,
StatusLike,
Subprotocol,
)
else:
Expand Down Expand Up @@ -176,6 +178,7 @@
"ExtensionParameter": ".typing",
"LoggerLike": ".typing",
"Origin": ".typing",
"StatusLike": "typing",
"Subprotocol": ".typing",
},
deprecated_aliases={
Expand Down
3 changes: 2 additions & 1 deletion src/websockets/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from typing import Optional

from . import datastructures, frames, http11
from .typing import StatusLike


__all__ = [
Expand Down Expand Up @@ -330,7 +331,7 @@ class AbortHandshake(InvalidHandshake):

def __init__(
self,
status: http.HTTPStatus,
status: StatusLike,
headers: datastructures.HeadersLike,
body: bytes = b"",
) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
from ..http import USER_AGENT
from ..protocol import State
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
from ..typing import ExtensionHeader, LoggerLike, Origin, StatusLike, Subprotocol
from .compatibility import asyncio_timeout
from .handshake import build_response, check_request
from .http import read_request
Expand All @@ -57,7 +57,7 @@

HeadersLikeOrCallable = Union[HeadersLike, Callable[[str, Headers], HeadersLike]]

HTTPResponse = Tuple[http.HTTPStatus, HeadersLike, bytes]
HTTPResponse = Tuple[StatusLike, HeadersLike, bytes]


class WebSocketServerProtocol(WebSocketCommonProtocol):
Expand Down Expand Up @@ -349,7 +349,7 @@ async def process_request(
request_headers: request headers.
Returns:
Optional[Tuple[http.HTTPStatus, HeadersLike, bytes]]: :obj:`None`
Optional[Tuple[StatusLike, HeadersLike, bytes]]: :obj:`None`
to continue the WebSocket handshake normally.
An HTTP response, represented by a 3-uple of the response status,
Expand Down Expand Up @@ -943,7 +943,7 @@ class Serve:
It defaults to ``"Python/x.y.z websockets/X.Y"``.
Setting it to :obj:`None` removes the header.
process_request (Optional[Callable[[str, Headers], \
Awaitable[Optional[Tuple[http.HTTPStatus, HeadersLike, bytes]]]]]):
Awaitable[Optional[Tuple[StatusLike, HeadersLike, bytes]]]]]):
Intercept HTTP request before the opening handshake.
See :meth:`~WebSocketServerProtocol.process_request` for details.
select_subprotocol: Select a subprotocol supported by the client.
Expand Down
3 changes: 2 additions & 1 deletion src/websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ExtensionHeader,
LoggerLike,
Origin,
StatusLike,
Subprotocol,
UpgradeProtocol,
)
Expand Down Expand Up @@ -480,7 +481,7 @@ def select_subprotocol(protocol, subprotocols):

def reject(
self,
status: http.HTTPStatus,
status: StatusLike,
text: str,
) -> Response:
"""
Expand Down
7 changes: 7 additions & 0 deletions src/websockets/typing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

import http
import logging
from typing import List, NewType, Optional, Tuple, Union


__all__ = [
"Data",
"LoggerLike",
"StatusLike",
"Origin",
"Subprotocol",
"ExtensionName",
Expand All @@ -30,6 +32,11 @@
"""Types accepted where a :class:`~logging.Logger` is expected."""


StatusLike = Union[http.HTTPStatus, int]
"""
Types accepted where an :class:`~http.HTTPStatus` is expected."""


Origin = NewType("Origin", str)
"""Value of a ``Origin`` header."""

Expand Down

0 comments on commit ed6cb1b

Please sign in to comment.