typing.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. import http
  3. import logging
  4. import os
  5. from typing import Any, NewType, Sequence
  6. __all__ = [
  7. "Data",
  8. "LoggerLike",
  9. "StatusLike",
  10. "Origin",
  11. "Subprotocol",
  12. "ExtensionName",
  13. "ExtensionParameter",
  14. ]
  15. # Public types used in the signature of public APIs
  16. Data = str | bytes
  17. """Types supported in a WebSocket message:
  18. :class:`str` for a Text_ frame, :class:`bytes` for a Binary_ frame.
  19. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  20. .. _Binary : https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  21. """
  22. BytesLike = bytes | bytearray | memoryview
  23. """Types accepted where :class:`bytes` is expected."""
  24. DataLike = str | bytes | bytearray | memoryview
  25. """Types accepted where :class:`Data` is expected."""
  26. LoggerLike = logging.Logger | logging.LoggerAdapter[Any]
  27. """Types accepted where a :class:`~logging.Logger` is expected."""
  28. PathLike = str | bytes | os.PathLike[str] | os.PathLike[bytes]
  29. """Types accepted where a filesystem path is expected."""
  30. StatusLike = http.HTTPStatus | int
  31. """Types accepted where an :class:`~http.HTTPStatus` is expected."""
  32. Origin = NewType("Origin", str)
  33. """Value of a ``Origin`` header."""
  34. Subprotocol = NewType("Subprotocol", str)
  35. """Subprotocol in a ``Sec-WebSocket-Protocol`` header."""
  36. ExtensionName = NewType("ExtensionName", str)
  37. """Name of a WebSocket extension."""
  38. ExtensionParameter = tuple[str, str | None]
  39. """Parameter of a WebSocket extension."""
  40. # Private types
  41. ExtensionHeader = tuple[ExtensionName, Sequence[ExtensionParameter]]
  42. """Extension in a ``Sec-WebSocket-Extensions`` header."""
  43. ConnectionOption = NewType("ConnectionOption", str)
  44. """Connection option in a ``Connection`` header."""
  45. UpgradeProtocol = NewType("UpgradeProtocol", str)
  46. """Upgrade protocol in an ``Upgrade`` header."""