Skip to content

Commit ed6cb1b

Browse files
committed
Make it explicit that status codes can be int.
The code and tests already support it but the types didn't reflect it. Refs #1406.
1 parent f62d44a commit ed6cb1b

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

docs/reference/types.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Types
77

88
.. autodata:: LoggerLike
99

10+
.. autodata:: StatusLike
11+
1012
.. autodata:: Origin
1113

1214
.. autodata:: Subprotocol

src/websockets/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"ExtensionName",
6262
"ExtensionParameter",
6363
"LoggerLike",
64+
"StatusLike",
6465
"Origin",
6566
"Subprotocol",
6667
]
@@ -115,6 +116,7 @@
115116
ExtensionParameter,
116117
LoggerLike,
117118
Origin,
119+
StatusLike,
118120
Subprotocol,
119121
)
120122
else:
@@ -176,6 +178,7 @@
176178
"ExtensionParameter": ".typing",
177179
"LoggerLike": ".typing",
178180
"Origin": ".typing",
181+
"StatusLike": "typing",
179182
"Subprotocol": ".typing",
180183
},
181184
deprecated_aliases={

src/websockets/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from typing import Optional
3535

3636
from . import datastructures, frames, http11
37+
from .typing import StatusLike
3738

3839

3940
__all__ = [
@@ -330,7 +331,7 @@ class AbortHandshake(InvalidHandshake):
330331

331332
def __init__(
332333
self,
333-
status: http.HTTPStatus,
334+
status: StatusLike,
334335
headers: datastructures.HeadersLike,
335336
body: bytes = b"",
336337
) -> None:

src/websockets/legacy/server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646
from ..http import USER_AGENT
4747
from ..protocol import State
48-
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
48+
from ..typing import ExtensionHeader, LoggerLike, Origin, StatusLike, Subprotocol
4949
from .compatibility import asyncio_timeout
5050
from .handshake import build_response, check_request
5151
from .http import read_request
@@ -57,7 +57,7 @@
5757

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

60-
HTTPResponse = Tuple[http.HTTPStatus, HeadersLike, bytes]
60+
HTTPResponse = Tuple[StatusLike, HeadersLike, bytes]
6161

6262

6363
class WebSocketServerProtocol(WebSocketCommonProtocol):
@@ -349,7 +349,7 @@ async def process_request(
349349
request_headers: request headers.
350350
351351
Returns:
352-
Optional[Tuple[http.HTTPStatus, HeadersLike, bytes]]: :obj:`None`
352+
Optional[Tuple[StatusLike, HeadersLike, bytes]]: :obj:`None`
353353
to continue the WebSocket handshake normally.
354354
355355
An HTTP response, represented by a 3-uple of the response status,
@@ -943,7 +943,7 @@ class Serve:
943943
It defaults to ``"Python/x.y.z websockets/X.Y"``.
944944
Setting it to :obj:`None` removes the header.
945945
process_request (Optional[Callable[[str, Headers], \
946-
Awaitable[Optional[Tuple[http.HTTPStatus, HeadersLike, bytes]]]]]):
946+
Awaitable[Optional[Tuple[StatusLike, HeadersLike, bytes]]]]]):
947947
Intercept HTTP request before the opening handshake.
948948
See :meth:`~WebSocketServerProtocol.process_request` for details.
949949
select_subprotocol: Select a subprotocol supported by the client.

src/websockets/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ExtensionHeader,
3333
LoggerLike,
3434
Origin,
35+
StatusLike,
3536
Subprotocol,
3637
UpgradeProtocol,
3738
)
@@ -480,7 +481,7 @@ def select_subprotocol(protocol, subprotocols):
480481

481482
def reject(
482483
self,
483-
status: http.HTTPStatus,
484+
status: StatusLike,
484485
text: str,
485486
) -> Response:
486487
"""

src/websockets/typing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3+
import http
34
import logging
45
from typing import List, NewType, Optional, Tuple, Union
56

67

78
__all__ = [
89
"Data",
910
"LoggerLike",
11+
"StatusLike",
1012
"Origin",
1113
"Subprotocol",
1214
"ExtensionName",
@@ -30,6 +32,11 @@
3032
"""Types accepted where a :class:`~logging.Logger` is expected."""
3133

3234

35+
StatusLike = Union[http.HTTPStatus, int]
36+
"""
37+
Types accepted where an :class:`~http.HTTPStatus` is expected."""
38+
39+
3340
Origin = NewType("Origin", str)
3441
"""Value of a ``Origin`` header."""
3542

0 commit comments

Comments
 (0)