Debian Codegraph @

src/websockets/typing.py

raw
1 from __future__ import annotations
2
3 import http
4 import logging
5 import os
6 from typing import Any, NewType, Sequence
7
8
9 __all__ = [
10 "Data",
11 "LoggerLike",
12 "StatusLike",
13 "Origin",
14 "Subprotocol",
15 "ExtensionName",
16 "ExtensionParameter",
17 ]
18
19
20 # Public types used in the signature of public APIs
21
22 Data = str | bytes
23 """Types supported in a WebSocket message:
24 :class:`str` for a Text_ frame, :class:`bytes` for a Binary_ frame.
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 BytesLike = bytes | bytearray | memoryview
32 """Types accepted where :class:`bytes` is expected."""
33
34 DataLike = str | bytes | bytearray | memoryview
35 """Types accepted where :class:`Data` is expected."""
36
37 LoggerLike = logging.Logger | logging.LoggerAdapter[Any]
38 """Types accepted where a :class:`~logging.Logger` is expected."""
39
40 PathLike = str | bytes | os.PathLike[str] | os.PathLike[bytes]
41 """Types accepted where a filesystem path is expected."""
42
43 StatusLike = http.HTTPStatus | int
44 """Types accepted where an :class:`~http.HTTPStatus` is expected."""
45
46
47 Origin = NewType("Origin", str)
48 """Value of a ``Origin`` header."""
49
50
51 Subprotocol = NewType("Subprotocol", str)
52 """Subprotocol in a ``Sec-WebSocket-Protocol`` header."""
53
54
55 ExtensionName = NewType("ExtensionName", str)
56 """Name of a WebSocket extension."""
57
58 ExtensionParameter = tuple[str, str | None]
59 """Parameter of a WebSocket extension."""
60
61
62 # Private types
63
64 ExtensionHeader = tuple[ExtensionName, Sequence[ExtensionParameter]]
65 """Extension in a ``Sec-WebSocket-Extensions`` header."""
66
67
68 ConnectionOption = NewType("ConnectionOption", str)
69 """Connection option in a ``Connection`` header."""
70
71
72 UpgradeProtocol = NewType("UpgradeProtocol", str)
73 """Upgrade protocol in an ``Upgrade`` header."""