loop.pyi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import asyncio
  2. import contextvars
  3. import ssl
  4. import sys
  5. from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
  6. from typing import (
  7. IO,
  8. Any,
  9. Awaitable,
  10. Callable,
  11. Dict,
  12. Generator,
  13. List,
  14. Optional,
  15. Sequence,
  16. Tuple,
  17. TypeVar,
  18. Union,
  19. overload,
  20. )
  21. _T = TypeVar('_T')
  22. _Context = Dict[str, Any]
  23. _ExceptionHandler = Callable[[asyncio.AbstractEventLoop, _Context], Any]
  24. _SSLContext = Union[bool, None, ssl.SSLContext]
  25. _ProtocolT = TypeVar("_ProtocolT", bound=asyncio.BaseProtocol)
  26. class Loop:
  27. def call_soon(
  28. self, callback: Callable[..., Any], *args: Any, context: Optional[Any] = ...
  29. ) -> asyncio.Handle: ...
  30. def call_soon_threadsafe(
  31. self, callback: Callable[..., Any], *args: Any, context: Optional[Any] = ...
  32. ) -> asyncio.Handle: ...
  33. def call_later(
  34. self, delay: float, callback: Callable[..., Any], *args: Any, context: Optional[Any] = ...
  35. ) -> asyncio.TimerHandle: ...
  36. def call_at(
  37. self, when: float, callback: Callable[..., Any], *args: Any, context: Optional[Any] = ...
  38. ) -> asyncio.TimerHandle: ...
  39. def time(self) -> float: ...
  40. def stop(self) -> None: ...
  41. def run_forever(self) -> None: ...
  42. def close(self) -> None: ...
  43. def get_debug(self) -> bool: ...
  44. def set_debug(self, enabled: bool) -> None: ...
  45. def is_running(self) -> bool: ...
  46. def is_closed(self) -> bool: ...
  47. def create_future(self) -> asyncio.Future[Any]: ...
  48. if sys.version_info >= (3, 13):
  49. def create_task(
  50. self,
  51. coro: Union[Awaitable[_T], Generator[Any, None, _T]],
  52. *,
  53. name: Optional[str] = ...,
  54. context: Optional[contextvars.Context] = ...,
  55. eager_start: Optional[bool] = ...,
  56. ) -> asyncio.Task[_T]: ...
  57. elif sys.version_info >= (3, 11):
  58. def create_task(
  59. self,
  60. coro: Union[Awaitable[_T], Generator[Any, None, _T]],
  61. *,
  62. name: Optional[str] = ...,
  63. context: Optional[contextvars.Context] = ...,
  64. ) -> asyncio.Task[_T]: ...
  65. else:
  66. def create_task(
  67. self,
  68. coro: Union[Awaitable[_T], Generator[Any, None, _T]],
  69. *,
  70. name: Optional[str] = ...,
  71. ) -> asyncio.Task[_T]: ...
  72. def set_task_factory(
  73. self,
  74. factory: Optional[
  75. Callable[[asyncio.AbstractEventLoop, Generator[Any, None, _T]], asyncio.Future[_T]]
  76. ],
  77. ) -> None: ...
  78. def get_task_factory(
  79. self,
  80. ) -> Optional[
  81. Callable[[asyncio.AbstractEventLoop, Generator[Any, None, _T]], asyncio.Future[_T]]
  82. ]: ...
  83. @overload
  84. def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ...
  85. @overload
  86. def run_until_complete(self, future: Awaitable[_T]) -> _T: ...
  87. async def getaddrinfo(
  88. self,
  89. host: Optional[Union[str, bytes]],
  90. port: Optional[Union[str, bytes, int]],
  91. *,
  92. family: int = ...,
  93. type: int = ...,
  94. proto: int = ...,
  95. flags: int = ...,
  96. ) -> List[
  97. Tuple[
  98. AddressFamily,
  99. SocketKind,
  100. int,
  101. str,
  102. Union[Tuple[str, int], Tuple[str, int, int, int]],
  103. ]
  104. ]: ...
  105. async def getnameinfo(
  106. self,
  107. sockaddr: Union[
  108. Tuple[str, int],
  109. Tuple[str, int, int],
  110. Tuple[str, int, int, int]
  111. ],
  112. flags: int = ...,
  113. ) -> Tuple[str, str]: ...
  114. async def start_tls(
  115. self,
  116. transport: asyncio.BaseTransport,
  117. protocol: asyncio.BaseProtocol,
  118. sslcontext: ssl.SSLContext,
  119. *,
  120. server_side: bool = ...,
  121. server_hostname: Optional[str] = ...,
  122. ssl_handshake_timeout: Optional[float] = ...,
  123. ssl_shutdown_timeout: Optional[float] = ...,
  124. ) -> asyncio.BaseTransport: ...
  125. @overload
  126. async def create_server(
  127. self,
  128. protocol_factory: asyncio.events._ProtocolFactory,
  129. host: Optional[Union[str, Sequence[str]]] = ...,
  130. port: int = ...,
  131. *,
  132. family: int = ...,
  133. flags: int = ...,
  134. sock: None = ...,
  135. backlog: int = ...,
  136. ssl: _SSLContext = ...,
  137. reuse_address: Optional[bool] = ...,
  138. reuse_port: Optional[bool] = ...,
  139. ssl_handshake_timeout: Optional[float] = ...,
  140. ssl_shutdown_timeout: Optional[float] = ...,
  141. start_serving: bool = ...,
  142. ) -> asyncio.AbstractServer: ...
  143. @overload
  144. async def create_server(
  145. self,
  146. protocol_factory: asyncio.events._ProtocolFactory,
  147. host: None = ...,
  148. port: None = ...,
  149. *,
  150. family: int = ...,
  151. flags: int = ...,
  152. sock: socket = ...,
  153. backlog: int = ...,
  154. ssl: _SSLContext = ...,
  155. reuse_address: Optional[bool] = ...,
  156. reuse_port: Optional[bool] = ...,
  157. ssl_handshake_timeout: Optional[float] = ...,
  158. ssl_shutdown_timeout: Optional[float] = ...,
  159. start_serving: bool = ...,
  160. ) -> asyncio.AbstractServer: ...
  161. @overload
  162. async def create_connection(
  163. self,
  164. protocol_factory: Callable[[], _ProtocolT],
  165. host: str = ...,
  166. port: int = ...,
  167. *,
  168. ssl: _SSLContext = ...,
  169. family: int = ...,
  170. proto: int = ...,
  171. flags: int = ...,
  172. sock: None = ...,
  173. local_addr: Optional[Tuple[str, int]] = ...,
  174. server_hostname: Optional[str] = ...,
  175. ssl_handshake_timeout: Optional[float] = ...,
  176. ssl_shutdown_timeout: Optional[float] = ...,
  177. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  178. @overload
  179. async def create_connection(
  180. self,
  181. protocol_factory: Callable[[], _ProtocolT],
  182. host: None = ...,
  183. port: None = ...,
  184. *,
  185. ssl: _SSLContext = ...,
  186. family: int = ...,
  187. proto: int = ...,
  188. flags: int = ...,
  189. sock: socket,
  190. local_addr: None = ...,
  191. server_hostname: Optional[str] = ...,
  192. ssl_handshake_timeout: Optional[float] = ...,
  193. ssl_shutdown_timeout: Optional[float] = ...,
  194. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  195. async def create_unix_server(
  196. self,
  197. protocol_factory: asyncio.events._ProtocolFactory,
  198. path: Optional[str] = ...,
  199. *,
  200. backlog: int = ...,
  201. sock: Optional[socket] = ...,
  202. ssl: _SSLContext = ...,
  203. ssl_handshake_timeout: Optional[float] = ...,
  204. ssl_shutdown_timeout: Optional[float] = ...,
  205. start_serving: bool = ...,
  206. ) -> asyncio.AbstractServer: ...
  207. async def create_unix_connection(
  208. self,
  209. protocol_factory: Callable[[], _ProtocolT],
  210. path: Optional[str] = ...,
  211. *,
  212. ssl: _SSLContext = ...,
  213. sock: Optional[socket] = ...,
  214. server_hostname: Optional[str] = ...,
  215. ssl_handshake_timeout: Optional[float] = ...,
  216. ssl_shutdown_timeout: Optional[float] = ...,
  217. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  218. def default_exception_handler(self, context: _Context) -> None: ...
  219. def get_exception_handler(self) -> Optional[_ExceptionHandler]: ...
  220. def set_exception_handler(self, handler: Optional[_ExceptionHandler]) -> None: ...
  221. def call_exception_handler(self, context: _Context) -> None: ...
  222. def add_reader(self, fd: Any, callback: Callable[..., Any], *args: Any) -> None: ...
  223. def remove_reader(self, fd: Any) -> None: ...
  224. def add_writer(self, fd: Any, callback: Callable[..., Any], *args: Any) -> None: ...
  225. def remove_writer(self, fd: Any) -> None: ...
  226. async def sock_recv(self, sock: socket, nbytes: int) -> bytes: ...
  227. async def sock_recv_into(self, sock: socket, buf: bytearray) -> int: ...
  228. async def sock_sendall(self, sock: socket, data: bytes) -> None: ...
  229. async def sock_accept(self, sock: socket) -> Tuple[socket, _RetAddress]: ...
  230. async def sock_connect(self, sock: socket, address: _Address) -> None: ...
  231. async def sock_recvfrom(self, sock: socket, bufsize: int) -> bytes: ...
  232. async def sock_recvfrom_into(self, sock: socket, buf: bytearray, nbytes: int = ...) -> int: ...
  233. async def sock_sendto(self, sock: socket, data: bytes, address: _Address) -> None: ...
  234. async def connect_accepted_socket(
  235. self,
  236. protocol_factory: Callable[[], _ProtocolT],
  237. sock: socket,
  238. *,
  239. ssl: _SSLContext = ...,
  240. ssl_handshake_timeout: Optional[float] = ...,
  241. ssl_shutdown_timeout: Optional[float] = ...,
  242. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  243. async def run_in_executor(
  244. self, executor: Any, func: Callable[..., _T], *args: Any
  245. ) -> _T: ...
  246. def set_default_executor(self, executor: Any) -> None: ...
  247. async def subprocess_shell(
  248. self,
  249. protocol_factory: Callable[[], _ProtocolT],
  250. cmd: Union[bytes, str],
  251. *,
  252. stdin: Any = ...,
  253. stdout: Any = ...,
  254. stderr: Any = ...,
  255. **kwargs: Any,
  256. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  257. async def subprocess_exec(
  258. self,
  259. protocol_factory: Callable[[], _ProtocolT],
  260. *args: Any,
  261. stdin: Any = ...,
  262. stdout: Any = ...,
  263. stderr: Any = ...,
  264. **kwargs: Any,
  265. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  266. async def connect_read_pipe(
  267. self, protocol_factory: Callable[[], _ProtocolT], pipe: Any
  268. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  269. async def connect_write_pipe(
  270. self, protocol_factory: Callable[[], _ProtocolT], pipe: Any
  271. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  272. def add_signal_handler(
  273. self, sig: int, callback: Callable[..., Any], *args: Any
  274. ) -> None: ...
  275. def remove_signal_handler(self, sig: int) -> bool: ...
  276. async def create_datagram_endpoint(
  277. self,
  278. protocol_factory: Callable[[], _ProtocolT],
  279. local_addr: Optional[Tuple[str, int]] = ...,
  280. remote_addr: Optional[Tuple[str, int]] = ...,
  281. *,
  282. family: int = ...,
  283. proto: int = ...,
  284. flags: int = ...,
  285. reuse_address: Optional[bool] = ...,
  286. reuse_port: Optional[bool] = ...,
  287. allow_broadcast: Optional[bool] = ...,
  288. sock: Optional[socket] = ...,
  289. ) -> tuple[asyncio.BaseProtocol, _ProtocolT]: ...
  290. async def shutdown_asyncgens(self) -> None: ...
  291. async def shutdown_default_executor(
  292. self,
  293. timeout: Optional[float] = ...,
  294. ) -> None: ...
  295. # Loop doesn't implement these, but since they are marked as abstract in typeshed,
  296. # we have to put them in so mypy thinks the base methods are overridden
  297. async def sendfile(
  298. self,
  299. transport: asyncio.BaseTransport,
  300. file: IO[bytes],
  301. offset: int = ...,
  302. count: Optional[int] = ...,
  303. *,
  304. fallback: bool = ...,
  305. ) -> int: ...
  306. async def sock_sendfile(
  307. self,
  308. sock: socket,
  309. file: IO[bytes],
  310. offset: int = ...,
  311. count: Optional[int] = ...,
  312. *,
  313. fallback: bool = ...
  314. ) -> int: ...