Skip to content

Commit d054951

Browse files
committed
fix: use 312
1 parent b6e2876 commit d054951

9 files changed

Lines changed: 64 additions & 110 deletions

File tree

src/socketio-stubs/_types.pyi

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@ from collections.abc import Awaitable, Callable, Mapping, Sequence
22
from contextlib import AbstractAsyncContextManager, AbstractContextManager
33
from threading import Event as ThreadingEvent
44
from types import ModuleType
5-
from typing import (
6-
Any,
7-
ClassVar,
8-
Concatenate,
9-
Literal,
10-
NotRequired,
11-
Required,
12-
TypeAlias,
13-
overload,
14-
)
5+
from typing import Any, ClassVar, Concatenate, Literal, NotRequired, Required, overload
156

167
import engineio
178
from _typeshed import Incomplete
@@ -27,17 +18,15 @@ from socketio.admin import InstrumentedServer
2718
from socketio.msgpack_packet import MsgPackPacket
2819
from socketio.server import Server
2920

30-
JsonType: TypeAlias = (
21+
type JsonType = (
3122
str | int | float | bool | None | Sequence[JsonType] | Mapping[str, JsonType]
3223
)
33-
DataType: TypeAlias = str | bytes | Sequence[JsonType] | Mapping[str, JsonType]
34-
TransportType: TypeAlias = Literal["websocket", "polling"]
35-
SocketIOModeType: TypeAlias = Literal["development", "production"]
36-
SyncAsyncModeType: TypeAlias = Literal[
37-
"eventlet", "gevent_uwsgi", "gevent", "threading"
38-
]
39-
AsyncAsyncModeType: TypeAlias = Literal["aiohttp", "sanic", "tornado", "asgi"]
40-
SerializerType: TypeAlias = Literal["default", "msgpack"]
24+
type DataType = str | bytes | Sequence[JsonType] | Mapping[str, JsonType]
25+
type TransportType = Literal["websocket", "polling"]
26+
type SocketIOModeType = Literal["development", "production"]
27+
type SyncAsyncModeType = Literal["eventlet", "gevent_uwsgi", "gevent", "threading"]
28+
type AsyncAsyncModeType = Literal["aiohttp", "sanic", "tornado", "asgi"]
29+
type SerializerType = Literal["default", "msgpack"]
4130

4231
class SessionContextManager(AbstractContextManager[Socket]):
4332
server: Server[Any]
@@ -225,27 +214,25 @@ class CustomMsgPackPacket(MsgPackPacket):
225214

226215
## handlers
227216

228-
ServerConnectHandler: TypeAlias = Callable[[str, dict[str, Any]], Any]
229-
ServerConnectHandlerWithData: TypeAlias = Callable[[str, dict[str, Any], Any], Any]
230-
ServerDisconnectHandler: TypeAlias = Callable[[str, engineio.Server.reason], Any]
231-
ServerDisconnectLegacyHandler: TypeAlias = Callable[[str], Any]
232-
ClientConnectHandler: TypeAlias = Callable[[], Any]
233-
ClientDisconnectHandler: TypeAlias = Callable[[engineio.Client.reason], Any]
234-
ClientDisconnectLegacyHandler: TypeAlias = Callable[[], Any]
235-
ClientConnectErrorHandler: TypeAlias = Callable[[Any], Any]
236-
CatchAllHandler: TypeAlias = Callable[[str, str, Any], Any]
237-
SyncEventHandlerWithSid: TypeAlias = Callable[
217+
type ServerConnectHandler = Callable[[str, dict[str, Any]], Any]
218+
type ServerConnectHandlerWithData = Callable[[str, dict[str, Any], Any], Any]
219+
type ServerDisconnectHandler = Callable[[str, engineio.Server.reason], Any]
220+
type ServerDisconnectLegacyHandler = Callable[[str], Any]
221+
type ClientConnectHandler = Callable[[], Any]
222+
type ClientDisconnectHandler = Callable[[engineio.Client.reason], Any]
223+
type ClientDisconnectLegacyHandler = Callable[[], Any]
224+
type ClientConnectErrorHandler = Callable[[Any], Any]
225+
type CatchAllHandler = Callable[[str, str, Any], Any]
226+
type SyncEventHandlerWithSid = Callable[
238227
Concatenate[str, ...], DataType | tuple[DataType, ...] | None
239228
]
240-
SyncEventHandlerWithoutSid: TypeAlias = Callable[
241-
[], DataType | tuple[DataType, ...] | None
242-
]
243-
SyncEventHandler: TypeAlias = SyncEventHandlerWithSid | SyncEventHandlerWithoutSid
244-
AsyncEventHandlerWithSid: TypeAlias = Callable[
229+
type SyncEventHandlerWithoutSid = Callable[[], DataType | tuple[DataType, ...] | None]
230+
type SyncEventHandler = SyncEventHandlerWithSid | SyncEventHandlerWithoutSid
231+
type AsyncEventHandlerWithSid = Callable[
245232
Concatenate[str, ...], Awaitable[DataType | tuple[DataType, ...] | None]
246233
]
247-
AsyncEventHandlerWithoutSid: TypeAlias = Callable[
234+
type AsyncEventHandlerWithoutSid = Callable[
248235
[], Awaitable[DataType | tuple[DataType, ...] | None]
249236
]
250-
AsyncEventHandler: TypeAlias = AsyncEventHandlerWithSid | AsyncEventHandlerWithoutSid
251-
EventHandler: TypeAlias = SyncEventHandler | AsyncEventHandler
237+
type AsyncEventHandler = AsyncEventHandlerWithSid | AsyncEventHandlerWithoutSid
238+
type EventHandler = SyncEventHandler | AsyncEventHandler

src/socketio-stubs/async_client.pyi

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import logging
33
from collections.abc import Awaitable, Callable
4-
from typing import Any, Literal, ParamSpec, TypeVar
4+
from typing import Any, Literal
55

66
import engineio
77
import requests
@@ -11,9 +11,6 @@ from socketio.async_namespace import AsyncClientNamespace
1111
from socketio.base_client import BaseClient
1212
from socketio.packet import Packet
1313

14-
_T = TypeVar("_T")
15-
_P = ParamSpec("_P")
16-
1714
default_logger: logging.Logger
1815

1916
class AsyncClient(
@@ -83,8 +80,8 @@ class AsyncClient(
8380
) -> tuple[Any, ...] | None: ...
8481
async def disconnect(self) -> None: ...
8582
async def shutdown(self) -> None: ...
86-
def start_background_task(
87-
self, target: Callable[_P, Awaitable[_T]], *args: _P.args, **kwargs: _P.kwargs
88-
) -> asyncio.Task[_T]: ...
83+
def start_background_task[**P, T](
84+
self, target: Callable[P, Awaitable[T]], *args: P.args, **kwargs: P.kwargs
85+
) -> asyncio.Task[T]: ...
8986
async def sleep(self, seconds: int = ...) -> None: ...
9087
def register_namespace(self, namespace_handler: AsyncClientNamespace) -> None: ...

src/socketio-stubs/async_server.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import logging
33
from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequence
4-
from typing import Any, Generic, Literal, NoReturn, ParamSpec, overload
4+
from typing import Any, Generic, Literal, NoReturn, overload
55

66
import engineio
77
from aiohttp.typedefs import LooseHeaders as AiohttpLooseHeaders
@@ -28,8 +28,6 @@ from socketio.async_namespace import AsyncNamespace
2828
from socketio.base_server import BaseServer
2929

3030
_A = TypeVar("_A", bound=AsyncAsyncModeType, default=Any)
31-
_P = ParamSpec("_P")
32-
_T = TypeVar("_T")
3331

3432
task_reference_holder: set[Any]
3533

@@ -191,9 +189,9 @@ class AsyncServer(BaseServer[Literal[True], engineio.AsyncServer], Generic[_A]):
191189
async def handle_request(
192190
self, *args: Any, **kwargs: Any
193191
) -> AiohttpResponse | SanicHTTPResponse | None: ...
194-
def start_background_task(
195-
self, target: Callable[_P, Awaitable[_T]], *args: _P.args, **kwargs: _P.kwargs
196-
) -> asyncio.Task[_T]: ...
192+
def start_background_task[**P, T](
193+
self, target: Callable[P, Awaitable[T]], *args: P.args, **kwargs: P.kwargs
194+
) -> asyncio.Task[T]: ...
197195
async def sleep(self, seconds: int = ...) -> None: ...
198196
def instrument(
199197
self,

src/socketio-stubs/base_client.pyi

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ from socketio._types import (
1313
ClientConnectErrorHandler,
1414
ClientConnectHandler,
1515
ClientDisconnectHandler,
16-
ClientDisconnectLegacyHandler,
1716
EventHandler,
1817
JsonModule,
1918
SerializerType,
@@ -27,14 +26,6 @@ _T_namespace = TypeVar(
2726
"_T_namespace", bound=BaseClientNamespace[Any], default=BaseClientNamespace[Any]
2827
)
2928
_IsAsyncio = TypeVar("_IsAsyncio", bound=bool, default=Literal[False])
30-
_F = TypeVar("_F", bound=Callable[..., Any])
31-
_F_event = TypeVar("_F_event", bound=EventHandler)
32-
_F_connect = TypeVar("_F_connect", bound=ClientConnectHandler)
33-
_F_connect_error = TypeVar("_F_connect_error", bound=ClientConnectErrorHandler)
34-
_F_disconnect = TypeVar(
35-
"_F_disconnect", bound=ClientDisconnectHandler | ClientDisconnectLegacyHandler
36-
)
37-
_F_catch_all = TypeVar("_F_catch_all", bound=CatchAllHandler)
3829

3930
default_logger: logging.Logger
4031
reconnecting_clients: list[BaseClient[Any]]
@@ -83,34 +74,34 @@ class BaseClient(Generic[_IsAsyncio, _T_co, _T_namespace]):
8374
) -> None: ...
8475
def is_asyncio_based(self) -> _IsAsyncio: ...
8576
@overload
86-
def on(
77+
def on[H: ClientConnectHandler](
8778
self,
8879
event: Literal["connect"],
8980
handler: None = ...,
9081
namespace: str | None = ...,
91-
) -> Callable[[_F_connect], _F_connect]: ...
82+
) -> Callable[[H], H]: ...
9283
@overload
93-
def on(
84+
def on[H: ClientConnectErrorHandler](
9485
self,
9586
event: Literal["connect_error"],
9687
handler: None = ...,
9788
namespace: str | None = ...,
98-
) -> Callable[[_F_connect_error], _F_connect_error]: ...
89+
) -> Callable[[H], H]: ...
9990
@overload
100-
def on(
91+
def on[H: ClientDisconnectHandler](
10192
self,
10293
event: Literal["disconnect"],
10394
handler: None = ...,
10495
namespace: str | None = ...,
105-
) -> Callable[[_F_disconnect], _F_disconnect]: ...
96+
) -> Callable[[H], H]: ...
10697
@overload
107-
def on(
98+
def on[H: CatchAllHandler](
10899
self, event: Literal["*"], handler: None = ..., namespace: str | None = ...
109-
) -> Callable[[_F_catch_all], _F_catch_all]: ...
100+
) -> Callable[[H], H]: ...
110101
@overload
111-
def on(
102+
def on[H: EventHandler](
112103
self, event: str, handler: None = ..., namespace: str | None = ...
113-
) -> Callable[[_F_event], _F_event]: ...
104+
) -> Callable[[H], H]: ...
114105
@overload
115106
def on(
116107
self,
@@ -119,16 +110,16 @@ class BaseClient(Generic[_IsAsyncio, _T_co, _T_namespace]):
119110
namespace: str | None = ...,
120111
) -> None: ...
121112
@overload
122-
def on(
113+
def on[F: Callable[..., Any]](
123114
self,
124115
event: str | Callable[..., Any],
125116
handler: Callable[..., Any] | None = ...,
126117
namespace: str | None = ...,
127-
) -> Callable[[_F], _F] | None: ...
118+
) -> Callable[[F], F] | None: ...
128119
@overload
129120
def event(self, handler: EventHandler) -> None: ...
130121
@overload
131-
def event(self, namespace: str | None) -> Callable[[_F_event], _F_event]: ...
122+
def event[H: EventHandler](self, namespace: str | None) -> Callable[[H], H]: ...
132123
def register_namespace(self, namespace_handler: _T_namespace) -> None: ...
133124
def get_sid(self, namespace: str | None = ...) -> str | None: ...
134125
def transport(self) -> TransportType: ...

src/socketio-stubs/base_server.pyi

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ from socketio._types import (
1313
JsonModule,
1414
SerializerType,
1515
ServerConnectHandler,
16-
ServerConnectHandlerWithData,
1716
ServerDisconnectHandler,
18-
ServerDisconnectLegacyHandler,
1917
SyncAsyncModeType,
2018
TransportType,
2119
)
@@ -24,15 +22,6 @@ from socketio.base_namespace import BaseClientNamespace
2422
from socketio.packet import Packet
2523

2624
_T_co = TypeVar("_T_co", bound=Server | AsyncServer, covariant=True, default=Any)
27-
_F = TypeVar("_F", bound=Callable[..., Any])
28-
_F_event = TypeVar("_F_event", bound=EventHandler)
29-
_F_connect = TypeVar(
30-
"_F_connect", bound=ServerConnectHandler | ServerConnectHandlerWithData
31-
)
32-
_F_disconnect = TypeVar(
33-
"_F_disconnect", bound=ServerDisconnectHandler | ServerDisconnectLegacyHandler
34-
)
35-
_F_catch_all = TypeVar("_F_catch_all", bound=CatchAllHandler)
3625
_IsAsyncio = TypeVar("_IsAsyncio", bound=bool, default=Literal[False])
3726

3827
default_logger: logging.Logger
@@ -66,27 +55,27 @@ class BaseServer(Generic[_IsAsyncio, _T_co]):
6655
) -> None: ...
6756
def is_asyncio_based(self) -> _IsAsyncio: ...
6857
@overload
69-
def on(
58+
def on[H: ServerConnectHandler](
7059
self,
7160
event: Literal["connect"],
7261
handler: None = ...,
7362
namespace: str | None = ...,
74-
) -> Callable[[_F_connect], _F_connect]: ...
63+
) -> Callable[[H], H]: ...
7564
@overload
76-
def on(
65+
def on[H: ServerDisconnectHandler](
7766
self,
7867
event: Literal["disconnect"],
7968
handler: None = ...,
8069
namespace: str | None = ...,
81-
) -> Callable[[_F_disconnect], _F_disconnect]: ...
70+
) -> Callable[[H], H]: ...
8271
@overload
83-
def on(
72+
def on[H: CatchAllHandler](
8473
self, event: Literal["*"], handler: None = ..., namespace: str | None = ...
85-
) -> Callable[[_F_catch_all], _F_catch_all]: ...
74+
) -> Callable[[H], H]: ...
8675
@overload
87-
def on(
76+
def on[H: EventHandler](
8877
self, event: str, handler: None = ..., namespace: str | None = ...
89-
) -> Callable[[_F_event], _F_event]: ...
78+
) -> Callable[[H], H]: ...
9079
@overload
9180
def on(
9281
self,
@@ -95,16 +84,16 @@ class BaseServer(Generic[_IsAsyncio, _T_co]):
9584
namespace: str | None = ...,
9685
) -> None: ...
9786
@overload
98-
def on(
87+
def on[F: Callable[..., Any]](
9988
self,
10089
event: str | Callable[..., Any],
10190
handler: Callable[..., Any] | None = ...,
10291
namespace: str | None = ...,
103-
) -> Callable[[_F], _F] | None: ...
92+
) -> Callable[[F], F] | None: ...
10493
@overload
10594
def event(self, handler: EventHandler, namespace: str | None = ...) -> None: ...
10695
@overload
107-
def event(self, namespace: str | None) -> Callable[[_F_event], _F_event]: ...
96+
def event[H: EventHandler](self, namespace: str | None) -> Callable[[H], H]: ...
10897
def register_namespace(
10998
self, namespace_handler: BaseClientNamespace[_IsAsyncio]
11099
) -> None: ...

src/socketio-stubs/client.pyi

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from collections.abc import Callable
33
from threading import Thread
4-
from typing import Any, Literal, ParamSpec, TypeVar
4+
from typing import Any, Literal
55

66
import engineio
77
import requests
@@ -11,9 +11,6 @@ from socketio.base_client import BaseClient
1111
from socketio.namespace import ClientNamespace
1212
from socketio.packet import Packet
1313

14-
_T = TypeVar("_T")
15-
_P = ParamSpec("_P")
16-
1714
class Client(BaseClient[Literal[False], engineio.Client, ClientNamespace]):
1815
connection_url: str # pyright: ignore[reportIncompatibleVariableOverride]
1916
connection_headers: dict[str, str] # pyright: ignore[reportIncompatibleVariableOverride]
@@ -79,8 +76,8 @@ class Client(BaseClient[Literal[False], engineio.Client, ClientNamespace]):
7976
) -> tuple[Any, ...] | None: ...
8077
def disconnect(self) -> None: ...
8178
def shutdown(self) -> None: ...
82-
def start_background_task(
83-
self, target: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs
79+
def start_background_task[**P](
80+
self, target: Callable[P, Any], *args: P.args, **kwargs: P.kwargs
8481
) -> Thread: ...
8582
def sleep(self, seconds: int = ...) -> None: ...
8683
def register_namespace(self, namespace_handler: ClientNamespace) -> None: ...

src/socketio-stubs/msgpack_packet.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from collections.abc import Callable
1+
from collections.abc import Buffer, Callable
22
from typing import Any
33

4-
from typing_extensions import Buffer
5-
64
from socketio._types import CustomMsgPackPacket
75
from socketio.packet import Packet
86

src/socketio-stubs/packet.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
from collections.abc import Buffer
12
from typing import Any, Literal
23

3-
from typing_extensions import Buffer
4-
54
from socketio._types import DataType, JsonModule
65

76
CONNECT: Literal[0]

src/socketio-stubs/server.pyi

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from collections.abc import Callable, Mapping
33
from threading import Thread
4-
from typing import Any, Generic, Literal, NoReturn, ParamSpec, overload
4+
from typing import Any, Generic, Literal, NoReturn, overload
55

66
import engineio
77
from typing_extensions import TypeVar
@@ -22,8 +22,6 @@ from socketio.namespace import Namespace
2222
from socketio.packet import Packet
2323

2424
_A = TypeVar("_A", bound=SyncAsyncModeType, default=Any)
25-
_P = ParamSpec("_P")
26-
_T = TypeVar("_T")
2725

2826
default_logger: logging.Logger
2927

@@ -115,8 +113,8 @@ class Server(BaseServer[Literal[False], engineio.Server], Generic[_A]):
115113
def handle_request(
116114
self, environ: Mapping[str, Any], start_response: Callable[[str, str], Any]
117115
) -> list[str | list[tuple[str, str]] | bytes]: ...
118-
def start_background_task(
119-
self, target: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs
116+
def start_background_task[**P](
117+
self, target: Callable[P, Any], *args: P.args, **kwargs: P.kwargs
120118
) -> Thread: ...
121119
def sleep(self, seconds: int = ...) -> None: ...
122120
def instrument(

0 commit comments

Comments
 (0)