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

fix: Better event listener type definitions #354

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
66 changes: 14 additions & 52 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ keywords = [
python = "^3.9"
apify-client = ">=1.8.1"
apify-shared = ">=1.1.2"
crawlee = "~0.4.0"
crawlee = { git = "https://github.com/apify/crawlee-python.git", branch = "improve-event-types" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change once we release the PR

cryptography = ">=42.0.0"
# TODO: relax the upper bound once the issue is resolved:
# https://github.com/apify/apify-sdk-python/issues/348
Expand Down
16 changes: 15 additions & 1 deletion src/apify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

from apify_shared.consts import WebhookEventType
from crawlee import Request
from crawlee.events._types import Event
from crawlee.events import (
Event,
EventAbortingData,
EventExitData,
EventListener,
EventMigratingData,
EventPersistStateData,
EventSystemInfoData,
)

from apify._actor import Actor
from apify._configuration import Configuration
Expand All @@ -15,6 +23,12 @@
'Actor',
'Configuration',
'Event',
'EventAbortingData',
'EventExitData',
'EventListener',
'EventMigratingData',
'EventPersistStateData',
'EventSystemInfoData',
'ProxyConfiguration',
'ProxyInfo',
'Request',
Expand Down
50 changes: 47 additions & 3 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import sys
from datetime import timedelta
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, cast, overload

from lazy_object_proxy import Proxy
from pydantic import AliasChoices
Expand All @@ -13,7 +13,15 @@
from apify_shared.consts import ActorEnvVars, ActorExitCodes, ApifyEnvVars
from apify_shared.utils import ignore_docs, maybe_extract_enum_member_value
from crawlee import service_container
from crawlee.events._types import Event, EventPersistStateData
from crawlee.events import (
Event,
EventAbortingData,
EventExitData,
EventListener,
EventMigratingData,
EventPersistStateData,
EventSystemInfoData,
)

from apify._configuration import Configuration
from apify._consts import EVENT_LISTENERS_TIMEOUT
Expand Down Expand Up @@ -470,7 +478,30 @@ async def set_value(
key_value_store = await self.open_key_value_store()
return await key_value_store.set_value(key, value, content_type=content_type)

def on(self, event_name: Event, listener: Callable) -> Callable:
@overload
def on(
self, event_name: Literal[Event.PERSIST_STATE], listener: EventListener[EventPersistStateData]
) -> EventListener[EventPersistStateData]: ...
@overload
def on(
self, event_name: Literal[Event.SYSTEM_INFO], listener: EventListener[EventSystemInfoData]
) -> EventListener[EventSystemInfoData]: ...
@overload
def on(
self, event_name: Literal[Event.MIGRATING], listener: EventListener[EventMigratingData]
) -> EventListener[EventMigratingData]: ...
@overload
def on(
self, event_name: Literal[Event.ABORTING], listener: EventListener[EventAbortingData]
) -> EventListener[EventAbortingData]: ...
@overload
def on(
self, event_name: Literal[Event.EXIT], listener: EventListener[EventExitData]
) -> EventListener[EventExitData]: ...
@overload
def on(self, event_name: Event, listener: EventListener[None]) -> EventListener[Any]: ...

def on(self, event_name: Event, listener: EventListener[Any]) -> EventListener[Any]:
"""Add an event listener to the Actor's event manager.

The following events can be emitted:
Expand Down Expand Up @@ -499,6 +530,19 @@ def on(self, event_name: Event, listener: Callable) -> Callable:
self._event_manager.on(event=event_name, listener=listener)
return listener

@overload
def off(self, event_name: Literal[Event.PERSIST_STATE], listener: EventListener[EventPersistStateData]) -> None: ...
@overload
def off(self, event_name: Literal[Event.SYSTEM_INFO], listener: EventListener[EventSystemInfoData]) -> None: ...
@overload
def off(self, event_name: Literal[Event.MIGRATING], listener: EventListener[EventMigratingData]) -> None: ...
@overload
def off(self, event_name: Literal[Event.ABORTING], listener: EventListener[EventAbortingData]) -> None: ...
@overload
def off(self, event_name: Literal[Event.EXIT], listener: EventListener[EventExitData]) -> None: ...
@overload
def off(self, event_name: Event, listener: EventListener[None]) -> None: ...

def off(self, event_name: Event, listener: Callable | None = None) -> None:
"""Remove a listener, or all listeners, from an Actor event.

Expand Down
Loading