Skip to content

Commit e63f985

Browse files
committed
Make Web Adapters Generic and accept a Client or any derivatives.
1 parent e042903 commit e63f985

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

twitchio/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import math
3030
from collections import defaultdict
3131
from types import MappingProxyType
32-
from typing import TYPE_CHECKING, Any, Literal, NamedTuple, Self, Unpack, overload
32+
from typing import TYPE_CHECKING, Any, Literal, NamedTuple, Self, Unpack, cast, overload
3333

3434
from .authentication import ManagedHTTPClient, Scopes, UserTokenPayload
3535
from .eventsub.enums import SubscriptionType
@@ -149,12 +149,14 @@ def __init__(
149149
msg = "If you require the StarletteAdapter please install the required packages: 'pip install twitchio[starlette]'."
150150
logger.warning(msg)
151151

152-
adapter: BaseAdapter | type[BaseAdapter] = options.get("adapter", AiohttpAdapter)
152+
adapter: BaseAdapter[Any] | type[BaseAdapter[Any]] | type[AiohttpAdapter[Self]] = options.get( # type: ignore
153+
"adapter", AiohttpAdapter
154+
)
153155
if isinstance(adapter, BaseAdapter):
154156
adapter.client = self
155157
self._adapter = adapter
156158
else:
157-
self._adapter = adapter()
159+
self._adapter = cast("AiohttpAdapter[Self]", adapter())
158160
self._adapter.client = self
159161

160162
# Own Client User. Set in login...
@@ -178,12 +180,12 @@ def __init__(
178180
self._setup_called = False
179181

180182
@property
181-
def adapter(self) -> BaseAdapter:
183+
def adapter(self) -> BaseAdapter[Any]:
182184
"""Property returning the :class:`~twitchio.AiohttpAdapter` or :class:`~twitchio.StarlettepAdapter` the bot is
183185
currently running."""
184186
return self._adapter
185187

186-
async def set_adapter(self, adapter: BaseAdapter) -> None:
188+
async def set_adapter(self, adapter: BaseAdapter[Any]) -> None:
187189
"""|coro|
188190
189191
Method which sets and starts a new web adapter which inherits from either :class:`~twitchio.AiohttpAdapter` or

twitchio/types_/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ClientOptions(TypedDict, total=False):
4343
redirect_uri: str | None
4444
scopes: Scopes | None
4545
session: aiohttp.ClientSession | None
46-
adapter: NotRequired[BaseAdapter]
46+
adapter: NotRequired[BaseAdapter[Any]]
4747
fetch_client_user: NotRequired[bool]
4848

4949

twitchio/web/aio_adapter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import datetime
2929
import logging
3030
from collections import deque
31-
from typing import TYPE_CHECKING, Any, cast
31+
from typing import TYPE_CHECKING, Any, TypeVar, cast
3232
from urllib.parse import unquote_plus
3333

3434
from aiohttp import web
@@ -54,10 +54,11 @@
5454
__all__ = ("AiohttpAdapter",)
5555

5656

57+
BT = TypeVar("BT", bound="Client")
5758
logger: logging.Logger = logging.getLogger(__name__)
5859

5960

60-
class AiohttpAdapter(BaseAdapter, web.Application):
61+
class AiohttpAdapter(BaseAdapter[BT], web.Application):
6162
"""The AiohttpAdapter for OAuth and Webhook based EventSub.
6263
6364
This adapter uses ``aiohttp`` which is a base dependency and should be installed and available with Twitchio.
@@ -141,7 +142,7 @@ def __init__(self) -> None:
141142
super().__init__(adapter=adapter)
142143
"""
143144

144-
client: Client
145+
client: BT
145146

146147
def __init__(
147148
self,
@@ -194,7 +195,7 @@ def __init__(
194195
self._responded: deque[str] = deque(maxlen=5000)
195196
self._running: bool = False
196197

197-
def __init_subclass__(cls: type[AiohttpAdapter]) -> None:
198+
def __init_subclass__(cls: type[AiohttpAdapter[BT]]) -> None:
198199
return
199200

200201
def __repr__(self) -> str:

twitchio/web/starlette_adapter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import datetime
2929
import logging
3030
from collections import deque
31-
from typing import TYPE_CHECKING, Any, cast
31+
from typing import TYPE_CHECKING, Any, TypeVar, cast
3232
from urllib.parse import unquote_plus
3333

3434
import uvicorn
@@ -57,10 +57,11 @@
5757
__all__ = ("StarletteAdapter",)
5858

5959

60+
BT = TypeVar("BT", bound="Client")
6061
logger: logging.Logger = logging.getLogger(__name__)
6162

6263

63-
class StarletteAdapter(BaseAdapter, Starlette):
64+
class StarletteAdapter(BaseAdapter[BT], Starlette):
6465
"""The StarletteAdapter for OAuth and Webhook based EventSub.
6566
6667
This adapter uses ``starlette`` which is an optional dependency and needs to be installed.
@@ -153,7 +154,7 @@ def __init__(self) -> None:
153154
super().__init__(adapter=adapter)
154155
"""
155156

156-
client: Client
157+
client: BT
157158

158159
def __init__(
159160
self,

twitchio/web/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import hashlib
2929
import hmac
3030
import logging
31-
from typing import TYPE_CHECKING, Any
31+
from typing import TYPE_CHECKING, Any, Generic, TypeVar
3232

3333
from aiohttp import web
3434

@@ -45,6 +45,7 @@
4545
from ..types_.eventsub import EventSubHeaders
4646

4747

48+
BT = TypeVar("BT", bound="Client")
4849
logger: logging.Logger = logging.getLogger(__name__)
4950

5051

@@ -82,8 +83,8 @@ def __init__(
8283
self.exception = exception
8384

8485

85-
class BaseAdapter(abc.ABC):
86-
client: Client
86+
class BaseAdapter(abc.ABC, Generic[BT]):
87+
client: BT
8788
_runner_task: asyncio.Task[None] | None
8889
_eventsub_secret: str | None
8990
_running: bool

0 commit comments

Comments
 (0)