typing.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import sys
  2. import warnings
  3. from typing import (
  4. Any,
  5. Awaitable,
  6. Callable,
  7. Dict,
  8. Iterable,
  9. List,
  10. Optional,
  11. Tuple,
  12. Type,
  13. Union,
  14. )
  15. from asgiref._pep562 import pep562
  16. if sys.version_info >= (3, 8):
  17. from typing import Literal, Protocol, TypedDict
  18. else:
  19. from typing_extensions import Literal, Protocol, TypedDict
  20. __all__ = (
  21. "ASGIVersions",
  22. "HTTPScope",
  23. "WebSocketScope",
  24. "LifespanScope",
  25. "WWWScope",
  26. "Scope",
  27. "HTTPRequestEvent",
  28. "HTTPResponseStartEvent",
  29. "HTTPResponseBodyEvent",
  30. "HTTPServerPushEvent",
  31. "HTTPDisconnectEvent",
  32. "WebSocketConnectEvent",
  33. "WebSocketAcceptEvent",
  34. "WebSocketReceiveEvent",
  35. "WebSocketSendEvent",
  36. "WebSocketResponseStartEvent",
  37. "WebSocketResponseBodyEvent",
  38. "WebSocketDisconnectEvent",
  39. "WebSocketCloseEvent",
  40. "LifespanStartupEvent",
  41. "LifespanShutdownEvent",
  42. "LifespanStartupCompleteEvent",
  43. "LifespanStartupFailedEvent",
  44. "LifespanShutdownCompleteEvent",
  45. "LifespanShutdownFailedEvent",
  46. "ASGIReceiveEvent",
  47. "ASGISendEvent",
  48. "ASGIReceiveCallable",
  49. "ASGISendCallable",
  50. "ASGI2Protocol",
  51. "ASGI2Application",
  52. "ASGI3Application",
  53. "ASGIApplication",
  54. )
  55. class ASGIVersions(TypedDict):
  56. spec_version: str
  57. version: Union[Literal["2.0"], Literal["3.0"]]
  58. class HTTPScope(TypedDict):
  59. type: Literal["http"]
  60. asgi: ASGIVersions
  61. http_version: str
  62. method: str
  63. scheme: str
  64. path: str
  65. raw_path: bytes
  66. query_string: bytes
  67. root_path: str
  68. headers: Iterable[Tuple[bytes, bytes]]
  69. client: Optional[Tuple[str, int]]
  70. server: Optional[Tuple[str, Optional[int]]]
  71. extensions: Optional[Dict[str, Dict[object, object]]]
  72. class WebSocketScope(TypedDict):
  73. type: Literal["websocket"]
  74. asgi: ASGIVersions
  75. http_version: str
  76. scheme: str
  77. path: str
  78. raw_path: bytes
  79. query_string: bytes
  80. root_path: str
  81. headers: Iterable[Tuple[bytes, bytes]]
  82. client: Optional[Tuple[str, int]]
  83. server: Optional[Tuple[str, Optional[int]]]
  84. subprotocols: Iterable[str]
  85. extensions: Optional[Dict[str, Dict[object, object]]]
  86. class LifespanScope(TypedDict):
  87. type: Literal["lifespan"]
  88. asgi: ASGIVersions
  89. WWWScope = Union[HTTPScope, WebSocketScope]
  90. Scope = Union[HTTPScope, WebSocketScope, LifespanScope]
  91. class HTTPRequestEvent(TypedDict):
  92. type: Literal["http.request"]
  93. body: bytes
  94. more_body: bool
  95. class HTTPResponseStartEvent(TypedDict):
  96. type: Literal["http.response.start"]
  97. status: int
  98. headers: Iterable[Tuple[bytes, bytes]]
  99. class HTTPResponseBodyEvent(TypedDict):
  100. type: Literal["http.response.body"]
  101. body: bytes
  102. more_body: bool
  103. class HTTPServerPushEvent(TypedDict):
  104. type: Literal["http.response.push"]
  105. path: str
  106. headers: Iterable[Tuple[bytes, bytes]]
  107. class HTTPDisconnectEvent(TypedDict):
  108. type: Literal["http.disconnect"]
  109. class WebSocketConnectEvent(TypedDict):
  110. type: Literal["websocket.connect"]
  111. class WebSocketAcceptEvent(TypedDict):
  112. type: Literal["websocket.accept"]
  113. subprotocol: Optional[str]
  114. headers: Iterable[Tuple[bytes, bytes]]
  115. class WebSocketReceiveEvent(TypedDict):
  116. type: Literal["websocket.receive"]
  117. bytes: Optional[bytes]
  118. text: Optional[str]
  119. class WebSocketSendEvent(TypedDict):
  120. type: Literal["websocket.send"]
  121. bytes: Optional[bytes]
  122. text: Optional[str]
  123. class WebSocketResponseStartEvent(TypedDict):
  124. type: Literal["websocket.http.response.start"]
  125. status: int
  126. headers: Iterable[Tuple[bytes, bytes]]
  127. class WebSocketResponseBodyEvent(TypedDict):
  128. type: Literal["websocket.http.response.body"]
  129. body: bytes
  130. more_body: bool
  131. class WebSocketDisconnectEvent(TypedDict):
  132. type: Literal["websocket.disconnect"]
  133. code: int
  134. class WebSocketCloseEvent(TypedDict):
  135. type: Literal["websocket.close"]
  136. code: int
  137. reason: Optional[str]
  138. class LifespanStartupEvent(TypedDict):
  139. type: Literal["lifespan.startup"]
  140. class LifespanShutdownEvent(TypedDict):
  141. type: Literal["lifespan.shutdown"]
  142. class LifespanStartupCompleteEvent(TypedDict):
  143. type: Literal["lifespan.startup.complete"]
  144. class LifespanStartupFailedEvent(TypedDict):
  145. type: Literal["lifespan.startup.failed"]
  146. message: str
  147. class LifespanShutdownCompleteEvent(TypedDict):
  148. type: Literal["lifespan.shutdown.complete"]
  149. class LifespanShutdownFailedEvent(TypedDict):
  150. type: Literal["lifespan.shutdown.failed"]
  151. message: str
  152. ASGIReceiveEvent = Union[
  153. HTTPRequestEvent,
  154. HTTPDisconnectEvent,
  155. WebSocketConnectEvent,
  156. WebSocketReceiveEvent,
  157. WebSocketDisconnectEvent,
  158. LifespanStartupEvent,
  159. LifespanShutdownEvent,
  160. ]
  161. ASGISendEvent = Union[
  162. HTTPResponseStartEvent,
  163. HTTPResponseBodyEvent,
  164. HTTPServerPushEvent,
  165. HTTPDisconnectEvent,
  166. WebSocketAcceptEvent,
  167. WebSocketSendEvent,
  168. WebSocketResponseStartEvent,
  169. WebSocketResponseBodyEvent,
  170. WebSocketCloseEvent,
  171. LifespanStartupCompleteEvent,
  172. LifespanStartupFailedEvent,
  173. LifespanShutdownCompleteEvent,
  174. LifespanShutdownFailedEvent,
  175. ]
  176. ASGIReceiveCallable = Callable[[], Awaitable[ASGIReceiveEvent]]
  177. ASGISendCallable = Callable[[ASGISendEvent], Awaitable[None]]
  178. class ASGI2Protocol(Protocol):
  179. def __init__(self, scope: Scope) -> None:
  180. ...
  181. async def __call__(
  182. self, receive: ASGIReceiveCallable, send: ASGISendCallable
  183. ) -> None:
  184. ...
  185. ASGI2Application = Type[ASGI2Protocol]
  186. ASGI3Application = Callable[
  187. [
  188. Scope,
  189. ASGIReceiveCallable,
  190. ASGISendCallable,
  191. ],
  192. Awaitable[None],
  193. ]
  194. ASGIApplication = Union[ASGI2Application, ASGI3Application]
  195. __deprecated__ = {
  196. "WebsocketConnectEvent": WebSocketConnectEvent,
  197. "WebsocketAcceptEvent": WebSocketAcceptEvent,
  198. "WebsocketReceiveEvent": WebSocketReceiveEvent,
  199. "WebsocketSendEvent": WebSocketSendEvent,
  200. "WebsocketResponseStartEvent": WebSocketResponseStartEvent,
  201. "WebsocketResponseBodyEvent": WebSocketResponseBodyEvent,
  202. "WebsocketDisconnectEvent": WebSocketDisconnectEvent,
  203. "WebsocketCloseEvent": WebSocketCloseEvent,
  204. }
  205. def __getattr__(name: str) -> Any:
  206. deprecated = __deprecated__.get(name)
  207. if deprecated:
  208. stacklevel = 3 if sys.version_info >= (3, 7) else 4
  209. warnings.warn(
  210. f"'{name}' is deprecated. Use '{deprecated.__name__}' instead.",
  211. category=DeprecationWarning,
  212. stacklevel=stacklevel,
  213. )
  214. return deprecated
  215. raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
  216. def __dir__() -> List[str]:
  217. return sorted(list(__all__) + list(__deprecated__.keys()))
  218. pep562(__name__)