Skip to content

Commit

Permalink
Allow for string annotations in listener signature (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Oct 10, 2023
1 parent 9eaf857 commit eb711c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jupyter_events/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,16 @@ async def listener_signature(logger: EventLogger, schema_id: str, data: dict) ->
"""An interface for a listener."""
...

async def listener_signature_str_ann(
logger: "EventLogger", schema_id: "str", data: "dict"
) -> "None":
"""An interface for a listener."""
...

expected_signature = inspect.signature(listener_signature)
expected_signature_str_ann = inspect.signature(listener_signature_str_ann)
# Assert this signature or raise an exception
if signature == expected_signature:
if signature in [expected_signature, expected_signature_str_ann]:
# If the schema ID and version is given, only add
# this modifier to that schema
if schema_id:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ async def my_listener(logger: EventLogger, schema_id: str, data: dict) -> None:
assert len(event_logger._active_listeners) == 0


async def test_listener_function_str_annotations(jp_event_logger, schema):
event_logger = jp_event_logger
listener_was_called = False

async def my_listener(logger: "EventLogger", schema_id: "str", data: "dict") -> "None":
nonlocal listener_was_called
listener_was_called = True

# Add the modifier
event_logger.add_listener(schema_id=schema.id, listener=my_listener)
event_logger.emit(schema_id=schema.id, data={"prop": "hello, world"})
await event_logger.gather_listeners()
assert listener_was_called
# Check that the active listeners are cleaned up.
assert len(event_logger._active_listeners) == 0


async def test_remove_listener_function(jp_event_logger, schema):
event_logger = jp_event_logger
listener_was_called = False
Expand Down

0 comments on commit eb711c3

Please sign in to comment.