Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for string annotations in listener signature #88

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading