typing.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. import http
  3. import logging
  4. from typing import TYPE_CHECKING, Any, NewType, Optional, Sequence, Union
  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. # Change to str | bytes when dropping Python < 3.10.
  16. Data = Union[str, bytes]
  17. """Types supported in a WebSocket message:
  18. :class:`str` for a Text_ frame, :class:`bytes` for a Binary_.
  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. # Change to logging.Logger | ... when dropping Python < 3.10.
  23. if TYPE_CHECKING:
  24. LoggerLike = Union[logging.Logger, logging.LoggerAdapter[Any]]
  25. """Types accepted where a :class:`~logging.Logger` is expected."""
  26. else: # remove this branch when dropping support for Python < 3.11
  27. LoggerLike = Union[logging.Logger, logging.LoggerAdapter]
  28. """Types accepted where a :class:`~logging.Logger` is expected."""
  29. # Change to http.HTTPStatus | int when dropping Python < 3.10.
  30. StatusLike = Union[http.HTTPStatus, int]
  31. """
  32. Types accepted where an :class:`~http.HTTPStatus` is expected."""
  33. Origin = NewType("Origin", str)
  34. """Value of a ``Origin`` header."""
  35. Subprotocol = NewType("Subprotocol", str)
  36. """Subprotocol in a ``Sec-WebSocket-Protocol`` header."""
  37. ExtensionName = NewType("ExtensionName", str)
  38. """Name of a WebSocket extension."""
  39. # Change to tuple[str, str | None] when dropping Python < 3.10.
  40. ExtensionParameter = tuple[str, Optional[str]]
  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."""