Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubs for Flask-SocketIO #10735

Merged
merged 26 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f4af7f4
Added stubs for Flask-SocketIO
sohang3112 Sep 19, 2023
885a3ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2023
7c4e3f9
Removed all Incomplete type stubs
sohang3112 Sep 19, 2023
d0c057c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2023
981ccc5
Type hint namespace as str
sohang3112 Sep 19, 2023
4d6db8b
Removed private classes and attributes, added kwargs types
sohang3112 Sep 20, 2023
9c8610e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2023
f694170
Deal with mypy errors
sohang3112 Sep 20, 2023
5d8fa96
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2023
ece6efa
Add method send in test_client.py
sohang3112 Sep 20, 2023
acd1a60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2023
b7e18e3
test_client: Corrected type hints
sohang3112 Sep 20, 2023
405251c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2023
59158cc
Fix some obvious stuff
AlexWaygood Sep 20, 2023
dc7d2b5
add the new allowlist
AlexWaygood Sep 20, 2023
9131d85
Update test_client.pyi
AlexWaygood Sep 20, 2023
e1ca935
Update __init__.pyi
AlexWaygood Sep 20, 2023
b6e8910
test_client.py: Corrected spelling mistake in method name get_received()
sohang3112 Sep 22, 2023
a4aaefb
Explicit type hints for kwargs
sohang3112 Sep 24, 2023
b9af134
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2023
c5b2e44
removed space in requirements
sohang3112 Sep 24, 2023
41d2fce
Removed BaseManager as its not needed any more
sohang3112 Sep 24, 2023
17fe22d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2023
f214048
many fixes
AlexWaygood Sep 25, 2023
7b67a60
another fix
AlexWaygood Sep 25, 2023
844214b
.
AlexWaygood Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"stubs/dateparser",
"stubs/docutils",
"stubs/Flask-Migrate",
"stubs/Flask-SocketIO",
"stubs/fpdf2",
"stubs/google-cloud-ndb",
"stubs/html5lib",
Expand Down
4 changes: 4 additions & 0 deletions stubs/Flask-SocketIO/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# private attributes / methods, not present in docs
flask_socketio.test_client.SocketIOTestClient.clients
flask_socketio.gevent_socketio_found
flask_socketio.call
3 changes: 3 additions & 0 deletions stubs/Flask-SocketIO/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = "5.3.*"
requires = ["Flask>=0.9"]
upstream_repository = "https://github.com/miguelgrinberg/flask-socketio"
133 changes: 133 additions & 0 deletions stubs/Flask-SocketIO/flask_socketio/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from _typeshed import Incomplete
from collections.abc import Callable
from threading import Thread
from typing import Any, Protocol, TypeVar, overload
from typing_extensions import ParamSpec, TypeAlias

from flask import Flask
from flask.testing import FlaskClient

from .namespace import Namespace
from .test_client import SocketIOTestClient

_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
_ExceptionHandler: TypeAlias = Callable[[BaseException], _R_co]
_Handler: TypeAlias = Callable[_P, _R_co]

class _HandlerDecorator(Protocol):
def __call__(self, handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ...

class _ExceptionHandlerDecorator(Protocol):
def __call__(self, exception_handler: _ExceptionHandler[_R_co]) -> _ExceptionHandler[_R_co]: ...

class SocketIO:
# Many instance attributes are deliberately not included here,
# as the maintainer of Flask-SocketIO considers them private, internal details:
# https://github.com/python/typeshed/pull/10735#discussion_r1330768869
def __init__(
self,
app: Flask | None = None,
*,
# SocketIO options
manage_session: bool = True,
message_queue: str | None = None,
channel: str = "flask-socketio",
path: str = "socket.io",
resource: str = "socket.io",
**kwargs, # TODO: Socket.IO server options, Engine.IO server config
) -> None: ...
def init_app(
self,
app: Flask,
*,
# SocketIO options
manage_session: bool = True,
message_queue: str | None = None,
channel: str = "flask-socketio",
path: str = "socket.io",
resource: str = "socket.io",
**kwargs, # TODO: Socket.IO server options, Engine.IO server config: ...
) -> None: ...
def on(self, message: str, namespace: str | None = None) -> _HandlerDecorator: ...
def on_error(self, namespace: str | None = None) -> _ExceptionHandlerDecorator: ...
def on_error_default(self, exception_handler: _ExceptionHandler[_R_co]) -> _ExceptionHandler[_R_co]: ...
def on_event(self, message: str, handler: _Handler[[Incomplete], object], namespace: str | None = None) -> None: ...
@overload
def event(self, __event_handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ...
@overload
def event(self, namespace: str | None = None, *args, **kwargs) -> _HandlerDecorator: ...
def on_namespace(self, namespace_handler: Namespace) -> None: ...
def emit(
self,
event: str,
*args,
namespace: str = "/", # / is the default (global) namespace
to: str | None = None,
include_self: bool = True,
skip_sid: str | list[str] | None = None,
callback: Callable[..., Incomplete] | None = None,
) -> None: ...
def call(
self,
event: str,
*args,
namespace: str = "/", # / is the default (global) namespace
to: str | None = None,
timeout: int = 60, # seconds
ignore_queue: bool = False,
): ...
def send(
self,
data: Any,
json: bool = False,
namespace: str | None = None,
to: str | None = None,
callback: Callable[..., Incomplete] | None = None,
include_self: bool = True,
skip_sid: list[str] | str | None = None,
**kwargs,
) -> None: ...
def close_room(self, room: str, namespace: str | None = None) -> None: ...
def run(
self,
app,
host: str | None = None,
port: int | None = None,
*,
debug: bool = True,
use_reloader: bool,
reloader_options: dict[str, Incomplete] = {},
log_output: bool,
allow_unsafe_werkzeug: bool = False,
**kwargs,
) -> None: ...
def stop(self) -> None: ...
def start_background_task(self, target: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> Thread: ...
def sleep(self, seconds: int = 0): ...
def test_client(
self,
app: Flask,
namespace: str | None = None,
query_string: str | None = None,
headers: dict[str, Incomplete] | None = None,
auth: dict[str, Incomplete] | None = None,
flask_test_client: FlaskClient | None = None,
) -> SocketIOTestClient: ...

def emit(
event,
*args,
namespace: str = "/", # / is the default (global) namespace
to: str | None = None,
include_self: bool = True,
skip_sid: str | list[str] | None = None,
callback: Callable[..., Incomplete] | None = None,
broadcast: bool = False,
) -> None: ...
def send(message: str, **kwargs) -> None: ...
def join_room(room, sid: str | None = None, namespace: str | None = None) -> None: ...
def leave_room(room, sid: str | None = None, namespace: str | None = None) -> None: ...
def close_room(room, namespace: str | None = None) -> None: ...
def rooms(sid: str | None = None, namespace: str | None = None) -> list[str]: ...
def disconnect(sid: str | None = None, namespace: str | None = None, silent: bool = False) -> None: ...
74 changes: 74 additions & 0 deletions stubs/Flask-SocketIO/flask_socketio/namespace.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from _typeshed import Incomplete
from collections.abc import Callable
from typing import Any, Protocol, TypeVar

_T = TypeVar("_T")

# at runtime, socketio.namespace.BaseNamespace, but socketio isn't py.typed
class _BaseNamespace(Protocol):
def is_asyncio_based(self) -> bool: ...
def trigger_event(self, event: str, *args): ...

# at runtime, socketio.namespace.BaseNamespace, but socketio isn't py.typed
class _Namespace(_BaseNamespace, Protocol):
def emit(
self,
event: str,
data: Incomplete | None = None,
to=None,
room: str | None = None,
skip_sid=None,
namespace: str | None = None,
callback: Callable[..., Incomplete] | None = None,
ignore_queue: bool = False,
): ...
def send(
self,
data: Incomplete,
to=None,
room: str | None = None,
skip_sid=None,
namespace: str | None = None,
callback: Callable[..., Incomplete] | None = None,
ignore_queue: bool = False,
) -> None: ...
def call(
self,
event: str,
data: Incomplete | None = None,
to=None,
sid=None,
namespace: str | None = None,
timeout=None,
ignore_queue: bool = False,
): ...
def enter_room(self, sid, room: str, namespace: str | None = None): ...
def leave_room(self, sid, room: str, namespace: str | None = None): ...
def close_room(self, room: str, namespace: str | None = None): ...
def rooms(self, sid, namespace: str | None = None): ...
def get_session(self, sid, namespace: str | None = None): ...
def save_session(self, sid, session, namespace: str | None = None): ...
def session(self, sid, namespace: str | None = None): ...
def disconnect(self, sid, namespace: str | None = None): ...

class Namespace(_Namespace):
def __init__(self, namespace: str | None = None) -> None: ...
def trigger_event(self, event: str, *args): ...
def emit( # type: ignore[override]
self,
event: str,
data: Incomplete | None = None,
room: str | None = None,
include_self: bool = True,
namespace: str | None = None,
callback: Callable[..., _T] | None = None,
) -> _T | tuple[str, int]: ...
def send( # type: ignore[override]
self,
data: Incomplete,
room: str | None = None,
include_self: bool = True,
namespace: str | None = None,
callback: Callable[..., Any] | None = None,
) -> None: ...
def close_room(self, room: str, namespace: str | None = None) -> None: ...
41 changes: 41 additions & 0 deletions stubs/Flask-SocketIO/flask_socketio/test_client.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from _typeshed import Incomplete
from typing import Any
from typing_extensions import TypedDict

from flask import Flask
from flask.testing import FlaskClient

class _Packet(TypedDict):
name: str
args: Any
namespace: str

class SocketIOTestClient:
def __init__(
self,
app: Flask,
socketio,
namespace: str | None = None,
query_string: str | None = None,
headers: dict[str, Incomplete] | None = None,
auth: dict[str, Incomplete] | None = None,
flask_test_client: FlaskClient | None = None,
) -> None: ...
def is_connected(self, namespace: str | None = None) -> bool: ...
def connect(
self,
namespace: str | None = None,
query_string: str | None = None,
headers: dict[str, Incomplete] | None = None,
auth: dict[str, Incomplete] | None = None,
) -> None: ...
def disconnect(self, namespace: str | None = None) -> None: ...
def emit(self, event: str, *args, callback: bool = True, namespace: str | None = None) -> Incomplete | None: ...
def send(
self,
data: str | dict[str, Incomplete] | list[Incomplete],
json: bool = False,
callback: bool = False,
namespace: str | None = None,
): ...
def get_received(self, namespace: str | None = None) -> list[_Packet]: ...