Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit e662ddf

Browse files
committed
refactor(dispatcher/event)!: parent -> force_parent
1 parent 4456eb1 commit e662ddf

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

discatcore/impl/dispatcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def has_event(self, name: str) -> bool:
8989
return name in self.events
9090

9191
def callback_for(
92-
self, event: str, *, one_shot: bool = False, parent: bool = False
92+
self, event: str, *, one_shot: bool = False, force_parent: bool = False
9393
) -> Callable[[CoroFunc], Event]:
9494
"""A shortcut decorator to add a callback to an event.
9595
If the event does not exist already, then a new one will be created.
9696
9797
Args:
9898
event: The name of the event to get or create.
9999
one_shot: Whether or not the callback should be a one shot (which means the callback will be removed after running). Defaults to False.
100-
parent: Whether or not this callback contains a self parameter. Defaults to False.
100+
force_parent: Whether or not this callback contains a self parameter. Defaults to False.
101101
102102
Returns:
103103
A wrapper function that acts as the actual decorator.
@@ -109,7 +109,7 @@ def wrapper(coro: CoroFunc):
109109
else:
110110
event_cls = self.events[event]
111111

112-
event_cls.add_callback(coro, one_shot=one_shot, parent=parent)
112+
event_cls.add_callback(coro, one_shot=one_shot, force_parent=force_parent)
113113
return event_cls
114114

115115
return wrapper

discatcore/impl/event.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,31 @@ class Event:
4747
"""
4848

4949
def __init__(self, name: str, parent: Dispatcher) -> None:
50-
self.name = name
51-
self.parent = parent
50+
self.name: str = name
51+
self.parent: Dispatcher = parent
5252
self.callbacks: list[CoroFunc] = []
5353
self.metadata: dict[CoroFunc, _EventCallbackMetadata] = {}
5454
self._proto: t.Optional[inspect.Signature] = None
5555
self._error_handler: CoroFunc = self.parent.error_handler
5656

5757
# setters/decorators
5858

59-
def set_proto(self, proto_func: Func[t.Any], *, parent: bool = False) -> None:
59+
def set_proto(
60+
self, proto_func: t.Union[Func[t.Any], staticmethod[t.Any]], *, force_parent: bool = False
61+
) -> None:
6062
"""Sets the prototype for this event.
6163
6264
Args:
6365
proto_func (Callable[..., t.Any]): The prototype for this event.
64-
parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
66+
force_parent (bool): Whether or not this callback contains a self parameter. Defaults to ``False``.
6567
"""
6668
is_static = isinstance(proto_func, staticmethod)
6769
if is_static:
6870
proto_func = proto_func.__func__
6971

7072
if not self._proto:
7173
sig = inspect.signature(proto_func)
72-
if parent and not is_static:
74+
if force_parent and not is_static:
7375
new_params = list(sig.parameters.values())
7476
new_params.pop(0)
7577
sig = sig.replace(parameters=new_params)
@@ -80,21 +82,24 @@ def set_proto(self, proto_func: Func[t.Any], *, parent: bool = False) -> None:
8082
raise ValueError(f"Event prototype for event {self.name} has already been set!")
8183

8284
def proto(
83-
self, func: t.Optional[Func[t.Any]] = None, *, parent: bool = False
85+
self,
86+
func: t.Optional[t.Union[Func[t.Any], staticmethod[t.Any]]] = None,
87+
*,
88+
force_parent: bool = False,
8489
) -> t.Union[Event, Callable[[Func[t.Any]], Event]]:
8590
"""A decorator to set the prototype of this event.
8691
8792
Args:
88-
func (t.Optional[Callable[..., t.Any]]): The prototype to pass into this decorator. Defaults to None.
89-
parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
93+
func (t.Optional[Callable[..., t.Any]]): The prototype to pass into this decorator. Defaults to ``None``.
94+
force_parent (bool): Whether or not this callback contains a self parameter. Defaults to ``False``.
9095
9196
Returns:
9297
Either this event object or a wrapper function that acts as the actual decorator.
9398
This depends on if the ``func`` arg was passed in.
9499
"""
95100

96101
def wrapper(func: Func[t.Any]):
97-
self.set_proto(func, parent=parent)
102+
self.set_proto(func, force_parent=force_parent)
98103
return self
99104

100105
if func:
@@ -134,16 +139,18 @@ def wrapper(func: CoroFunc):
134139

135140
return wrapper
136141

137-
def add_callback(self, func: CoroFunc, *, one_shot: bool = False, parent: bool = False) -> None:
142+
def add_callback(
143+
self, func: CoroFunc, *, one_shot: bool = False, force_parent: bool = False
144+
) -> None:
138145
"""Adds a new callback to this event.
139146
140147
Args:
141148
func (Callable[..., Coroutine[t.Any, t.Any, t.Any]]): The callback to add to this event.
142149
one_shot (bool): Whether or not the callback should be a one shot (which means the callback will be removed after running). Defaults to False.
143-
parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
150+
force_parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
144151
"""
145152
if not self._proto:
146-
self.set_proto(func, parent=parent)
153+
self.set_proto(func, force_parent=force_parent)
147154
# this is to prevent static type checkers from inferring that self._proto is
148155
# still None after setting it indirectly via a different function
149156
# (it should never go here tho because exceptions stop the flow of this code
@@ -155,7 +162,7 @@ def add_callback(self, func: CoroFunc, *, one_shot: bool = False, parent: bool =
155162
raise TypeError("Callback provided is not a coroutine.")
156163

157164
callback_sig = inspect.signature(func)
158-
if parent:
165+
if force_parent:
159166
new_params = list(callback_sig.parameters.values())
160167
new_params.pop(0)
161168
callback_sig = callback_sig.replace(parameters=new_params)
@@ -184,22 +191,26 @@ def remove_callback(self, index: int) -> None:
184191
_log.debug("Removed event callback with index %d under event %s", index, self.name)
185192

186193
def callback(
187-
self, func: t.Optional[CoroFunc] = None, *, one_shot: bool = False, parent: bool = False
194+
self,
195+
func: t.Optional[CoroFunc] = None,
196+
*,
197+
one_shot: bool = False,
198+
force_parent: bool = False,
188199
) -> t.Union[Event, Callable[[Func[t.Any]], Event]]:
189200
"""A decorator to add a callback to this event.
190201
191202
Args:
192203
func (t.Optional[Callable[..., Coroutine[t.Any, t.Any, t.Any]]]): The function to pass into this decorator. Defaults to None.
193204
one_shot (bool): Whether or not the callback should be a one shot (which means the callback will be removed after running). Defaults to False.
194-
parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
205+
force_parent (bool): Whether or not this callback contains a self parameter. Defaults to False.
195206
196207
Returns:
197208
Either this event object or a wrapper function that acts as the actual decorator.
198209
This depends on if the ``func`` arg was passed in.
199210
"""
200211

201212
def wrapper(func: CoroFunc):
202-
self.add_callback(func, one_shot=one_shot, parent=parent)
213+
self.add_callback(func, one_shot=one_shot, force_parent=force_parent)
203214
return self
204215

205216
if func:

0 commit comments

Comments
 (0)