typing.py 1.9 KB

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