|
| 1 | +from _typeshed import Incomplete |
| 2 | +from collections.abc import Callable |
| 3 | +from threading import Thread |
| 4 | +from typing import Any, Protocol, TypeVar, overload |
| 5 | +from typing_extensions import ParamSpec, TypeAlias |
| 6 | + |
| 7 | +from flask import Flask |
| 8 | +from flask.testing import FlaskClient |
| 9 | + |
| 10 | +from .namespace import Namespace |
| 11 | +from .test_client import SocketIOTestClient |
| 12 | + |
| 13 | +_P = ParamSpec("_P") |
| 14 | +_R_co = TypeVar("_R_co", covariant=True) |
| 15 | +_ExceptionHandler: TypeAlias = Callable[[BaseException], _R_co] |
| 16 | +_Handler: TypeAlias = Callable[_P, _R_co] |
| 17 | + |
| 18 | +class _HandlerDecorator(Protocol): |
| 19 | + def __call__(self, handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ... |
| 20 | + |
| 21 | +class _ExceptionHandlerDecorator(Protocol): |
| 22 | + def __call__(self, exception_handler: _ExceptionHandler[_R_co]) -> _ExceptionHandler[_R_co]: ... |
| 23 | + |
| 24 | +class SocketIO: |
| 25 | + # Many instance attributes are deliberately not included here, |
| 26 | + # as the maintainer of Flask-SocketIO considers them private, internal details: |
| 27 | + # https://github.com/python/typeshed/pull/10735#discussion_r1330768869 |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + app: Flask | None = None, |
| 31 | + *, |
| 32 | + # SocketIO options |
| 33 | + manage_session: bool = True, |
| 34 | + message_queue: str | None = None, |
| 35 | + channel: str = "flask-socketio", |
| 36 | + path: str = "socket.io", |
| 37 | + resource: str = "socket.io", |
| 38 | + **kwargs, # TODO: Socket.IO server options, Engine.IO server config |
| 39 | + ) -> None: ... |
| 40 | + def init_app( |
| 41 | + self, |
| 42 | + app: Flask, |
| 43 | + *, |
| 44 | + # SocketIO options |
| 45 | + manage_session: bool = True, |
| 46 | + message_queue: str | None = None, |
| 47 | + channel: str = "flask-socketio", |
| 48 | + path: str = "socket.io", |
| 49 | + resource: str = "socket.io", |
| 50 | + **kwargs, # TODO: Socket.IO server options, Engine.IO server config: ... |
| 51 | + ) -> None: ... |
| 52 | + def on(self, message: str, namespace: str | None = None) -> _HandlerDecorator: ... |
| 53 | + def on_error(self, namespace: str | None = None) -> _ExceptionHandlerDecorator: ... |
| 54 | + def on_error_default(self, exception_handler: _ExceptionHandler[_R_co]) -> _ExceptionHandler[_R_co]: ... |
| 55 | + def on_event(self, message: str, handler: _Handler[[Incomplete], object], namespace: str | None = None) -> None: ... |
| 56 | + @overload |
| 57 | + def event(self, __event_handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ... |
| 58 | + @overload |
| 59 | + def event(self, namespace: str | None = None, *args, **kwargs) -> _HandlerDecorator: ... |
| 60 | + def on_namespace(self, namespace_handler: Namespace) -> None: ... |
| 61 | + def emit( |
| 62 | + self, |
| 63 | + event: str, |
| 64 | + *args, |
| 65 | + namespace: str = "/", # / is the default (global) namespace |
| 66 | + to: str | None = None, |
| 67 | + include_self: bool = True, |
| 68 | + skip_sid: str | list[str] | None = None, |
| 69 | + callback: Callable[..., Incomplete] | None = None, |
| 70 | + ) -> None: ... |
| 71 | + def call( |
| 72 | + self, |
| 73 | + event: str, |
| 74 | + *args, |
| 75 | + namespace: str = "/", # / is the default (global) namespace |
| 76 | + to: str | None = None, |
| 77 | + timeout: int = 60, # seconds |
| 78 | + ignore_queue: bool = False, |
| 79 | + ): ... |
| 80 | + def send( |
| 81 | + self, |
| 82 | + data: Any, |
| 83 | + json: bool = False, |
| 84 | + namespace: str | None = None, |
| 85 | + to: str | None = None, |
| 86 | + callback: Callable[..., Incomplete] | None = None, |
| 87 | + include_self: bool = True, |
| 88 | + skip_sid: list[str] | str | None = None, |
| 89 | + **kwargs, |
| 90 | + ) -> None: ... |
| 91 | + def close_room(self, room: str, namespace: str | None = None) -> None: ... |
| 92 | + def run( |
| 93 | + self, |
| 94 | + app, |
| 95 | + host: str | None = None, |
| 96 | + port: int | None = None, |
| 97 | + *, |
| 98 | + debug: bool = True, |
| 99 | + use_reloader: bool, |
| 100 | + reloader_options: dict[str, Incomplete] = {}, |
| 101 | + log_output: bool, |
| 102 | + allow_unsafe_werkzeug: bool = False, |
| 103 | + **kwargs, |
| 104 | + ) -> None: ... |
| 105 | + def stop(self) -> None: ... |
| 106 | + def start_background_task(self, target: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> Thread: ... |
| 107 | + def sleep(self, seconds: int = 0): ... |
| 108 | + def test_client( |
| 109 | + self, |
| 110 | + app: Flask, |
| 111 | + namespace: str | None = None, |
| 112 | + query_string: str | None = None, |
| 113 | + headers: dict[str, Incomplete] | None = None, |
| 114 | + auth: dict[str, Incomplete] | None = None, |
| 115 | + flask_test_client: FlaskClient | None = None, |
| 116 | + ) -> SocketIOTestClient: ... |
| 117 | + |
| 118 | +def emit( |
| 119 | + event, |
| 120 | + *args, |
| 121 | + namespace: str = "/", # / is the default (global) namespace |
| 122 | + to: str | None = None, |
| 123 | + include_self: bool = True, |
| 124 | + skip_sid: str | list[str] | None = None, |
| 125 | + callback: Callable[..., Incomplete] | None = None, |
| 126 | + broadcast: bool = False, |
| 127 | +) -> None: ... |
| 128 | +def send(message: str, **kwargs) -> None: ... |
| 129 | +def join_room(room, sid: str | None = None, namespace: str | None = None) -> None: ... |
| 130 | +def leave_room(room, sid: str | None = None, namespace: str | None = None) -> None: ... |
| 131 | +def close_room(room, namespace: str | None = None) -> None: ... |
| 132 | +def rooms(sid: str | None = None, namespace: str | None = None) -> list[str]: ... |
| 133 | +def disconnect(sid: str | None = None, namespace: str | None = None, silent: bool = False) -> None: ... |
0 commit comments