Skip to content

Commit 6b3849b

Browse files
committed
type the commands ext to mypy
1 parent 5d61794 commit 6b3849b

File tree

7 files changed

+106
-83
lines changed

7 files changed

+106
-83
lines changed

twitchio/ext/commands/bot.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25+
from __future__ import annotations
2526
import importlib
2627
import inspect
2728
import sys
2829
import traceback
29-
from typing import Callable, Optional, Union, Coroutine
30+
import types
31+
from typing import Callable, Optional, Union, Coroutine, Dict, List, TYPE_CHECKING, Mapping, Awaitable
3032

3133
from twitchio.client import Client
32-
from .core import *
34+
from .core import Command, Group, Context
3335
from .errors import *
3436
from .meta import Cog
3537
from .stringparser import StringParser
3638
from .utils import _CaseInsensitiveDict
3739

40+
if TYPE_CHECKING:
41+
from twitchio import Message
3842

3943
class Bot(Client):
4044
def __init__(
@@ -54,15 +58,15 @@ def __init__(
5458
self._prefix = prefix
5559

5660
if kwargs.get("case_insensitive", False):
57-
self._commands = _CaseInsensitiveDict()
58-
self._command_aliases = _CaseInsensitiveDict()
61+
self._commands: Union[dict, _CaseInsensitiveDict] = _CaseInsensitiveDict()
62+
self._command_aliases: Union[dict, _CaseInsensitiveDict] = _CaseInsensitiveDict()
5963
else:
6064
self._commands = {}
6165
self._command_aliases = {}
6266

63-
self._modules = {}
64-
self._cogs = {}
65-
self._checks = []
67+
self._modules: Dict[str, types.ModuleType] = {}
68+
self._cogs: Dict[str, Cog] = {}
69+
self._checks: List[Callable[[Context], Union[bool, Awaitable[bool]]]] = []
6670

6771
self.__init__commands__()
6872

@@ -259,7 +263,7 @@ async def invoke(self, context):
259263
self.run_event("command_invoke", context)
260264
await context.command(context)
261265

262-
def load_module(self, name: str):
266+
def load_module(self, name: str) -> None:
263267
"""Method which loads a module and it's cogs.
264268
265269
Parameters
@@ -273,15 +277,15 @@ def load_module(self, name: str):
273277
module = importlib.import_module(name)
274278

275279
if hasattr(module, "prepare"):
276-
module.prepare(self)
280+
module.prepare(self) # type: ignore
277281
else:
278282
del module
279283
del sys.modules[name]
280284
raise ImportError(f"Module <{name}> is missing a prepare method")
281285

282286
self._modules[name] = module
283287

284-
def unload_module(self, name: str):
288+
def unload_module(self, name: str) -> None:
285289
"""Method which unloads a module and it's cogs.
286290
287291
Parameters
@@ -296,7 +300,7 @@ def unload_module(self, name: str):
296300

297301
if hasattr(module, "breakdown"):
298302
try:
299-
module.breakdown(self)
303+
module.breakdown(self) # type: ignore
300304
except:
301305
pass
302306

@@ -341,7 +345,7 @@ def reload_module(self, name: str):
341345
self.load_module(name)
342346
except Exception as e:
343347
sys.modules.update(modules)
344-
module.prepare(self)
348+
module.prepare(self) # type: ignore
345349
raise
346350

347351
def add_cog(self, cog: Cog):
@@ -411,7 +415,7 @@ async def my_command(self, ctx):
411415
"""
412416
pass
413417

414-
async def global_after_invoke(self, ctx):
418+
async def global_after_invoke(self, ctx: Context) -> None:
415419
"""|coro|
416420
417421
Method which is called after any command is invoked regardless if it failed or not.
@@ -435,11 +439,11 @@ def commands(self):
435439
return self._commands
436440

437441
@property
438-
def cogs(self):
442+
def cogs(self) -> Mapping[str, Cog]:
439443
"""The currently loaded cogs."""
440444
return self._cogs
441445

442-
async def event_command_error(self, context, error):
446+
async def event_command_error(self, context: Context, error: Exception) -> None:
443447
"""|coro|
444448
445449
Event called when an error occurs during command invocation.
@@ -454,7 +458,7 @@ async def event_command_error(self, context, error):
454458
print(f"Ignoring exception in command: {error}:", file=sys.stderr)
455459
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
456460

457-
async def event_message(self, message):
461+
async def event_message(self, message: Message) -> None:
458462
"""|coro|
459463
460464
Event called when a PRIVMSG is received from Twitch.

twitchio/ext/commands/builtin_converter.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25+
from __future__ import annotations
2526
import re
2627
from typing import TYPE_CHECKING
2728

@@ -42,7 +43,7 @@
4243
)
4344

4445

45-
async def convert_Chatter(ctx: "Context", arg: str):
46+
async def convert_Chatter(ctx: Context, arg: str) -> Chatter:
4647
"""
4748
Converts the argument into a chatter in the chat. If the chatter is not found, BadArgument is raised.
4849
"""
@@ -54,14 +55,14 @@ async def convert_Chatter(ctx: "Context", arg: str):
5455
return resp[0]
5556

5657

57-
async def convert_PartialChatter(ctx: "Context", arg: str):
58+
async def convert_PartialChatter(ctx: Context, arg: str) -> Chatter:
5859
"""
5960
Actually a shorthand to :ref:`~convert_Chatter`
6061
"""
61-
return convert_Chatter(ctx, arg)
62+
return await convert_Chatter(ctx, arg)
6263

6364

64-
async def convert_Clip(ctx: "Context", arg: str):
65+
async def convert_Clip(ctx: Context, arg: str) -> Clip:
6566
finder = re.search(r"(https://clips.twitch.tv/)?(?P<slug>.*)", arg)
6667
if not finder:
6768
raise RuntimeError("regex failed to match") # this should never ever raise, but its here to make type checkers happy
@@ -74,7 +75,7 @@ async def convert_Clip(ctx: "Context", arg: str):
7475
return clips[0]
7576

7677

77-
async def convert_User(ctx: "Context", arg: str):
78+
async def convert_User(ctx: Context, arg: str) -> User:
7879
"""
7980
Similar to convert_Chatter, but fetches from the twitch API instead,
8081
returning a :class:`twitchio.User` instead of a :class:`twitchio.Chatter`.
@@ -87,14 +88,14 @@ async def convert_User(ctx: "Context", arg: str):
8788
return user[0]
8889

8990

90-
async def convert_PartialUser(ctx: "Context", arg: str):
91+
async def convert_PartialUser(ctx: Context, arg: str) -> User:
9192
"""
9293
This is simply a shorthand to :ref:`~convert_User`, as fetching from the api will return a full user model
9394
"""
9495
return await convert_User(ctx, arg)
9596

9697

97-
async def convert_Channel(ctx: "Context", arg: str):
98+
async def convert_Channel(ctx: Context, arg: str) -> Channel:
9899
if arg not in ctx.bot._connection._cache:
99100
raise BadArgument(f"Not connected to channel '{arg}'")
100101

twitchio/ext/commands/cooldowns.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
DEALINGS IN THE SOFTWARE.
2525
"""
2626

27+
from __future__ import annotations
2728
import enum
2829
import time
2930

0 commit comments

Comments
 (0)