Skip to content

Commit a4292dd

Browse files
committed
Move plugin hooks to typed dict
1 parent 134a2fe commit a4292dd

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

cloudbot/plugin.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
from functools import partial
88
from operator import attrgetter
99
from pathlib import Path
10-
from typing import Dict, List, MutableMapping, Optional, Tuple, Type, cast
10+
from typing import (
11+
Dict,
12+
List,
13+
MutableMapping,
14+
Optional,
15+
Tuple,
16+
Type,
17+
TypedDict,
18+
cast,
19+
)
1120
from weakref import WeakValueDictionary
1221

1322
import sqlalchemy
@@ -19,8 +28,18 @@
1928
CommandHook,
2029
ConfigHook,
2130
EventHook,
31+
IrcOutHook,
32+
OnCapAckHook,
33+
OnCapAvaliableHook,
34+
OnConnectHook,
35+
OnStartHook,
36+
OnStopHook,
37+
PeriodicHook,
38+
PermHook,
39+
PostHookHook,
2240
RawHook,
2341
RegexHook,
42+
SieveHook,
2443
hook_name_to_plugin,
2544
)
2645
from cloudbot.util import HOOK_ATTR, LOADED_ATTR, async_util, database
@@ -29,7 +48,25 @@
2948
logger = logging.getLogger("cloudbot")
3049

3150

32-
def find_hooks(parent, module):
51+
class HookDict(TypedDict):
52+
command: List[CommandHook]
53+
on_connect: List[OnConnectHook]
54+
on_start: List[OnStartHook]
55+
on_stop: List[OnStopHook]
56+
on_cap_available: List[OnCapAvaliableHook]
57+
on_cap_ack: List[OnCapAckHook]
58+
sieve: List[SieveHook]
59+
event: List[EventHook]
60+
regex: List[RegexHook]
61+
periodic: List[PeriodicHook]
62+
irc_raw: List[RawHook]
63+
irc_out: List[IrcOutHook]
64+
post_hook: List[PostHookHook]
65+
config: List[ConfigHook]
66+
perm_check: List[PermHook]
67+
68+
69+
def find_hooks(parent, module) -> HookDict:
3370
hooks = defaultdict(list)
3471
for func in module.__dict__.values():
3572
if hasattr(func, HOOK_ATTR) and not hasattr(func, "_not_" + HOOK_ATTR):
@@ -44,7 +81,7 @@ def find_hooks(parent, module):
4481
# delete the hook to free memory
4582
delattr(func, HOOK_ATTR)
4683

47-
return hooks
84+
return cast(HookDict, hooks)
4885

4986

5087
def find_tables(code):
@@ -351,7 +388,7 @@ async def load_plugin(self, path):
351388
self._sort_hooks()
352389

353390
# we don't need this anymore
354-
del plugin.hooks["on_start"]
391+
plugin.hooks["on_start"].clear()
355392

356393
def _sort_hooks(self) -> None:
357394
def _sort_list(hooks):

cloudbot/util/web.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
from typing import Dict, Optional, Union
2121

2222
import requests
23-
from requests import HTTPError, PreparedRequest, RequestException, Response
24-
from requests import Request
23+
from requests import (
24+
HTTPError,
25+
PreparedRequest,
26+
Request,
27+
RequestException,
28+
Response,
29+
)
2530

2631
# Constants
2732
DEFAULT_SHORTENER = "is.gd"
@@ -163,7 +168,9 @@ def paste(data, ext="txt", service=DEFAULT_PASTEBIN, raise_on_no_paste=False):
163168

164169

165170
class ServiceError(Exception):
166-
def __init__(self, request: Union[Request, PreparedRequest], message: str) -> None:
171+
def __init__(
172+
self, request: Union[Request, PreparedRequest], message: str
173+
) -> None:
167174
super().__init__(message)
168175
self.request = request
169176

0 commit comments

Comments
 (0)