Skip to content

Commit eeaa7c2

Browse files
authored
Add Generic to Context (#506)
1 parent 18a915d commit eeaa7c2

File tree

7 files changed

+69
-51
lines changed

7 files changed

+69
-51
lines changed

twitchio/ext/commands/bot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from twitchio.user import User
5252

5353
from .components import Component
54-
from .types_ import AutoBotOptions, BotOptions
54+
from .types_ import AutoBotOptions, BotOptions, BotT
5555

5656
PrefixT: TypeAlias = str | Iterable[str] | Callable[["Bot", "ChatMessage"], Coroutine[Any, Any, str | Iterable[str]]]
5757

@@ -433,15 +433,15 @@ async def test(ctx: CustomContext) -> None:
433433
async def _process_commands(
434434
self, payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate
435435
) -> None:
436-
ctx: Context = self.get_context(payload)
436+
ctx = self.get_context(payload)
437437
await self.invoke(ctx)
438438

439439
async def process_commands(
440440
self, payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate
441441
) -> None:
442442
await self._process_commands(payload)
443443

444-
async def invoke(self, ctx: Context) -> None:
444+
async def invoke(self, ctx: Context[BotT]) -> None:
445445
try:
446446
await ctx.invoke()
447447
except CommandError as e:
@@ -482,7 +482,7 @@ async def event_command_error(self, payload: CommandErrorPayload) -> None:
482482
msg = f'Ignoring exception in command "{payload.context.command}":\n'
483483
logger.error(msg, exc_info=payload.exception)
484484

485-
async def before_invoke(self, ctx: Context) -> None:
485+
async def before_invoke(self, ctx: Context[BotT]) -> None:
486486
"""A pre invoke hook for all commands that have been added to the bot.
487487
488488
Commands from :class:`~.commands.Component`'s are included, however if you wish to control them separately,
@@ -513,7 +513,7 @@ async def before_invoke(self, ctx: Context) -> None:
513513
The context associated with command invocation, before being passed to the command.
514514
"""
515515

516-
async def after_invoke(self, ctx: Context) -> None:
516+
async def after_invoke(self, ctx: Context[BotT]) -> None:
517517
"""A post invoke hook for all commands that have been added to the bot.
518518
519519
Commands from :class:`~.commands.Component`'s are included, however if you wish to control them separately,
@@ -544,7 +544,7 @@ async def after_invoke(self, ctx: Context) -> None:
544544
The context associated with command invocation, after being passed through the command.
545545
"""
546546

547-
async def global_guard(self, ctx: Context, /) -> bool:
547+
async def global_guard(self, ctx: Context[BotT], /) -> bool:
548548
"""|coro|
549549
550550
A global guard applied to all commmands added to the bot.

twitchio/ext/commands/components.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from collections.abc import Callable
3838

3939
from .context import Context
40-
from .types_ import ComponentOptions
40+
from .types_ import BotT, ComponentOptions
4141

4242

4343
__all__ = ("Component",)
@@ -262,13 +262,13 @@ async def component_teardown(self) -> None:
262262
This method is intended to be overwritten, by default it does nothing.
263263
"""
264264

265-
async def component_before_invoke(self, ctx: Context) -> None:
265+
async def component_before_invoke(self, ctx: Context[BotT]) -> None:
266266
"""Hook called before a :class:`~.commands.Command` in this Component is invoked.
267267
268268
Similar to :meth:`~.commands.Bot.before_invoke` but only applies to commands in this Component.
269269
"""
270270

271-
async def component_after_invoke(self, ctx: Context) -> None:
271+
async def component_after_invoke(self, ctx: Context[BotT]) -> None:
272272
"""Hook called after a :class:`~.commands.Command` has successfully invoked in this Component.
273273
274274
Similar to :meth:`~.commands.Bot.after_invoke` but only applies to commands in this Component.

twitchio/ext/commands/context.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
from __future__ import annotations
2626

2727
from collections.abc import Iterable
28-
from typing import TYPE_CHECKING, Any, Literal, TypeAlias
28+
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeAlias
2929

3030
from twitchio.models.eventsub_ import ChannelPointsRedemptionAdd, ChannelPointsRedemptionUpdate, ChatMessage
3131

3232
from .core import CommandErrorPayload, ContextType, RewardCommand, RewardStatus
3333
from .exceptions import *
34+
from .types_ import BotT
3435
from .view import StringView
3536

3637

@@ -50,7 +51,7 @@
5051
PrefixT: TypeAlias = str | Iterable[str] | Callable[[Bot, ChatMessage], Coroutine[Any, Any, str | Iterable[str]]]
5152

5253

53-
class Context:
54+
class Context(Generic[BotT]):
5455
"""The Context class constructed when a message or reward redemption in the respective events is received and processed
5556
in a :class:`~.commands.Bot`.
5657
@@ -77,10 +78,10 @@ def __init__(
7778
self,
7879
payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate,
7980
*,
80-
bot: Bot,
81+
bot: BotT,
8182
) -> None:
8283
self._payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate = payload
83-
self._bot: Bot = bot
84+
self._bot = bot
8485
self._component: Component | None = None
8586
self._prefix: str | None = None
8687

@@ -220,7 +221,7 @@ def channel(self) -> PartialUser:
220221
return self.broadcaster
221222

222223
@property
223-
def bot(self) -> Bot:
224+
def bot(self) -> BotT:
224225
"""Property returning the :class:`~.commands.Bot` object."""
225226
return self._bot
226227

twitchio/ext/commands/converters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
if TYPE_CHECKING:
3535
from .bot import Bot
3636
from .context import Context
37+
from .types_ import BotT
3738

3839
__all__ = ("_BaseConverter",)
3940

@@ -67,7 +68,7 @@ def _bool(self, arg: str) -> bool:
6768

6869
return result
6970

70-
async def _user(self, context: Context, arg: str) -> User:
71+
async def _user(self, context: Context[BotT], arg: str) -> User:
7172
arg = arg.lower()
7273
users: list[User]
7374
msg: str = 'Failed to convert "{}" to User. A User with the ID or login could not be found.'

twitchio/ext/commands/cooldowns.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import twitchio
3737

3838
from .context import Context
39+
from .types_ import BotT
3940

4041

4142
__all__ = ("BaseCooldown", "Bucket", "BucketType", "Cooldown", "GCRACooldown")
@@ -65,7 +66,7 @@ class BucketType(enum.Enum):
6566
channel = 2
6667
chatter = 3
6768

68-
def get_key(self, payload: twitchio.ChatMessage | Context) -> Any:
69+
def get_key(self, payload: twitchio.ChatMessage | Context[BotT]) -> Any:
6970
if self is BucketType.user:
7071
return payload.chatter.id
7172

@@ -75,7 +76,7 @@ def get_key(self, payload: twitchio.ChatMessage | Context) -> Any:
7576
elif self is BucketType.chatter:
7677
return (payload.broadcaster.id, payload.chatter.id)
7778

78-
def __call__(self, payload: twitchio.ChatMessage | Context) -> Any:
79+
def __call__(self, payload: twitchio.ChatMessage | Context[BotT]) -> Any:
7980
return self.get_key(payload)
8081

8182

0 commit comments

Comments
 (0)