|
2 | 2 | Events |
3 | 3 | ~~~~~~ |
4 | 4 |
|
5 | | -A simple framework publish events or callbacks from a class to subscribed listener(s). |
| 5 | +A simple framework to publish events or callbacks to subscribed listener(s). |
6 | 6 |
|
7 | 7 | Async events/callbacks are also supported via the `AsyncEvent` and `AsyncCallback` |
8 | 8 | descriptors. |
9 | 9 |
|
10 | | -Event can have multiple listening functions where callbacks can only have a single |
11 | | -function bound (assigning a second will remove the previous). |
| 10 | +An event can have multiple listening functions where as callbacks can only have |
| 11 | +a single function bound (assigning a second will remove the previous binding). |
| 12 | +
|
| 13 | +.. note:: |
| 14 | +
|
| 15 | + Event and Callback descriptors do not work when ``__slots__`` are defined. |
| 16 | + If slots are defined a ``InstanceHasNoDictError`` will be raised on access |
| 17 | +
|
12 | 18 |
|
13 | 19 | Example:: |
14 | 20 |
|
@@ -49,8 +55,9 @@ async def on_new_message(message: str): |
49 | 55 | from typing import TypeVar |
50 | 56 | from typing import Union |
51 | 57 |
|
52 | | -__all__ = ("Event", "AsyncEvent", "listen_to", "Callback", "AsyncCallback", "bind_to") |
| 58 | +from pyapp.exceptions import UnsupportedObject |
53 | 59 |
|
| 60 | +__all__ = ("Event", "AsyncEvent", "listen_to", "Callback", "AsyncCallback", "bind_to") |
54 | 61 |
|
55 | 62 | _CT = TypeVar("_CT") |
56 | 63 |
|
@@ -148,6 +155,10 @@ def __get__(self, instance, owner) -> ListenerSet[_CT]: |
148 | 155 | return listeners |
149 | 156 |
|
150 | 157 | def __set_name__(self, owner, name): |
| 158 | + if hasattr(owner, "__slots__"): |
| 159 | + raise UnsupportedObject( |
| 160 | + "An Event cannot be used on an object with __slots__ defined." |
| 161 | + ) |
151 | 162 | self.name = name # pylint: disable=attribute-defined-outside-init |
152 | 163 |
|
153 | 164 |
|
@@ -260,6 +271,10 @@ def __get__(self, instance, owner) -> CallbackBinding[_CT]: |
260 | 271 | return wrapper |
261 | 272 |
|
262 | 273 | def __set_name__(self, owner, name): |
| 274 | + if hasattr(owner, "__slots__"): |
| 275 | + raise UnsupportedObject( |
| 276 | + "A Callback cannot be used on an object with __slots__ defined." |
| 277 | + ) |
263 | 278 | self.name = name # pylint: disable=attribute-defined-outside-init |
264 | 279 |
|
265 | 280 |
|
|
0 commit comments