Skip to content

Commit

Permalink
Fix linting on a9f98d2
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Oct 21, 2024
1 parent 058b743 commit 16a639d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/niobot/_event_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import typing as t

if t.TYPE_CHECKING:
from . import CommandError, Context, MatrixRoom, RoomMessage, SyncResponse, Event
from . import CommandError, Context, Event, MatrixRoom, RoomMessage, SyncResponse


async def event_loop_running() -> t.Optional[t.Any]:
Expand Down Expand Up @@ -129,22 +129,22 @@ async def on_command_error(ctx, error):
async def raw(room: "MatrixRoom", event: "Event") -> t.Optional[t.Any]:
"""
This is a special event that is handled when you directly pass a `niobot.Event` to `on_event`.
You cannot listen to this in the traditional sense of "on_event('name')" as it is not a named event.
But, this extensibility allows you to listen directly for events not covered by the library.
??? example
The below code will listen directly for the redaction event and will print out the redaction details.
See [the nio events](https://matrix-nio.readthedocs.io/en/latest/nio.html#module-nio.events) documentation
for more details and a list of available events.x
```python
import niobot
@bot.on_event(niobot.RedactionEvent) # listen for redactions
async def on_redaction(room, event):
print(f"{event.sender} redacted {event.redacts} for {event.reason!r} in {room.display_name}")
```
```
"""
...
4 changes: 3 additions & 1 deletion src/niobot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,17 @@ def decorator(func):
def add_event_listener(self, event_type: typing.Union[str, nio.Event], func):
self._events.setdefault(event_type, [])
self._events[event_type].append(func)

if isinstance(event_type, nio.Event):

@functools.wraps(func)
async def event_safety_wrapper(*args):
# This is necessary to stop callbacks crashing the process
try:
return await func(*args)
except Exception as e:
self.log.exception("Error in raw event listener %r", func, exc_info=e)

func = event_safety_wrapper
self.add_event_callback(func, event_type)
self.log.debug("Added raw event listener %r for %r", func, event_type)
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_attachment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pathlib
import typing

import niobot
import pytest

import niobot

ASSETS = pathlib.Path(__file__).parent / "assets"


Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import typing

import niobot
import pytest

import niobot

DEFAULT_HELP_COMMAND = niobot.Command(
"help",
niobot.DefaultHelpCommand().respond,
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typing

import pytest

from niobot.commands import Argument
from niobot.context import Context
from niobot.utils.parsers import *
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_utils_unblocking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from niobot.utils import unblocking


Expand Down

0 comments on commit 16a639d

Please sign in to comment.