1from __future__ import annotations
2
3import http
4import logging
5from typing import TYPE_CHECKING, Any, NewType, Optional, Sequence, Union
6
7
8__all__ = [
9 "Data",
10 "LoggerLike",
11 "StatusLike",
12 "Origin",
13 "Subprotocol",
14 "ExtensionName",
15 "ExtensionParameter",
16]
17
18
19# Public types used in the signature of public APIs
20
21# Change to str | bytes when dropping Python < 3.10.
22Data = Union[str, bytes]
23"""Types supported in a WebSocket message:
24:class:`str` for a Text_ frame, :class:`bytes` for a Binary_.
25
26.. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
27.. _Binary : https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
28
29"""
30
31
32# Change to logging.Logger | ... when dropping Python < 3.10.
33if TYPE_CHECKING:
34 LoggerLike = Union[logging.Logger, logging.LoggerAdapter[Any]]
35 """Types accepted where a :class:`~logging.Logger` is expected."""
36else: # remove this branch when dropping support for Python < 3.11
37 LoggerLike = Union[logging.Logger, logging.LoggerAdapter]
38 """Types accepted where a :class:`~logging.Logger` is expected."""
39
40
41# Change to http.HTTPStatus | int when dropping Python < 3.10.
42StatusLike = Union[http.HTTPStatus, int]
43"""
44Types accepted where an :class:`~http.HTTPStatus` is expected."""
45
46
47Origin = NewType("Origin", str)
48"""Value of a ``Origin`` header."""
49
50
51Subprotocol = NewType("Subprotocol", str)
52"""Subprotocol in a ``Sec-WebSocket-Protocol`` header."""
53
54
55ExtensionName = NewType("ExtensionName", str)
56"""Name of a WebSocket extension."""
57
58# Change to tuple[str, str | None] when dropping Python < 3.10.
59ExtensionParameter = tuple[str, Optional[str]]
60"""Parameter of a WebSocket extension."""
61
62
63# Private types
64
65ExtensionHeader = tuple[ExtensionName, Sequence[ExtensionParameter]]
66"""Extension in a ``Sec-WebSocket-Extensions`` header."""
67
68
69ConnectionOption = NewType("ConnectionOption", str)
70"""Connection option in a ``Connection`` header."""
71
72
73UpgradeProtocol = NewType("UpgradeProtocol", str)
74"""Upgrade protocol in an ``Upgrade`` header."""