Skip to content
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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ keywords = [
"scraping",
]
dependencies = [
"apify-client<2.0.0",
"apify-shared<2.0.1",
"apify-client>=2.0.0,<3.0.0",
"apify-shared>=2.0.0,<3.0.0",
"crawlee@git+https://github.com/apify/crawlee-python.git@master",
"cachetools>=5.5.0",
"cryptography>=42.0.0",
Expand Down
3 changes: 1 addition & 2 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from apify_client import ApifyClientAsync
from apify_shared.consts import ActorEnvVars, ActorExitCodes, ApifyEnvVars
from apify_shared.utils import maybe_extract_enum_member_value
from crawlee import service_locator
from crawlee.events import (
Event,
Expand All @@ -31,7 +30,7 @@
from apify._crypto import decrypt_input_secrets, load_private_key
from apify._models import ActorRun
from apify._proxy_configuration import ProxyConfiguration
from apify._utils import docs_group, docs_name, get_system_info, is_running_in_ipython
from apify._utils import docs_group, docs_name, get_system_info, is_running_in_ipython, maybe_extract_enum_member_value
from apify.events import ApifyEventManager, EventManager, LocalEventManager
from apify.log import _configure_logging, logger
from apify.storage_clients import ApifyStorageClient
Expand Down
10 changes: 9 additions & 1 deletion src/apify/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import builtins
import sys
from enum import Enum
from importlib import metadata
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING, Any, Literal

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -81,3 +82,10 @@ def wrapper(func: Callable) -> Callable:
return func

return wrapper


def maybe_extract_enum_member_value(maybe_enum_member: Any) -> Any:
"""Extract the value of an enumeration member if it is an Enum, otherwise return the original value."""
if isinstance(maybe_enum_member, Enum):
return maybe_enum_member.value
return maybe_enum_member
4 changes: 2 additions & 2 deletions src/apify/storage_clients/_apify/_dataset_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async def get_data(
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: str | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
flatten: list[str] | None = None,
Expand Down Expand Up @@ -229,7 +229,7 @@ async def iterate_items(
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: str | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
) -> AsyncIterator[dict]:
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/test_actor_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ async def main() -> None:

assert env_dict.get('is_at_home') is True
assert env_dict.get('token') is not None
assert env_dict.get('actor_events_ws_url') is not None
assert env_dict.get('events_websocket_url') is not None
assert env_dict.get('input_key') is not None

assert len(env_dict.get('actor_id', '')) == 17
assert len(env_dict.get('actor_run_id', '')) == 17
assert len(env_dict.get('id', '')) == 17
assert len(env_dict.get('build_id', '')) == 17
assert len(env_dict.get('run_id', '')) == 17
assert len(env_dict.get('user_id', '')) == 17

actor = await make_actor(label='get-env', main_func=main)
Expand Down
27 changes: 1 addition & 26 deletions tests/unit/actor/test_actor_env_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,15 @@ async def test_actor_is_not_at_home_when_local() -> None:
assert is_at_home is False


async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch) -> None: # noqa: PLR0912
async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch) -> None:
ignored_env_vars = {
ApifyEnvVars.INPUT_KEY,
ApifyEnvVars.MEMORY_MBYTES,
ApifyEnvVars.STARTED_AT,
ApifyEnvVars.TIMEOUT_AT,
ApifyEnvVars.DEFAULT_DATASET_ID,
ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID,
ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID,
ApifyEnvVars.SDK_LATEST_VERSION,
ApifyEnvVars.LOG_FORMAT,
ApifyEnvVars.LOG_LEVEL,
ActorEnvVars.STANDBY_PORT,
ApifyEnvVars.PERSIST_STORAGE,
}

legacy_env_vars = {
ApifyEnvVars.ACT_ID: ActorEnvVars.ID,
ApifyEnvVars.ACT_RUN_ID: ActorEnvVars.RUN_ID,
ApifyEnvVars.ACTOR_ID: ActorEnvVars.ID,
ApifyEnvVars.ACTOR_BUILD_ID: ActorEnvVars.BUILD_ID,
ApifyEnvVars.ACTOR_BUILD_NUMBER: ActorEnvVars.BUILD_NUMBER,
ApifyEnvVars.ACTOR_RUN_ID: ActorEnvVars.RUN_ID,
ApifyEnvVars.ACTOR_TASK_ID: ActorEnvVars.TASK_ID,
ApifyEnvVars.CONTAINER_URL: ActorEnvVars.WEB_SERVER_URL,
ApifyEnvVars.CONTAINER_PORT: ActorEnvVars.WEB_SERVER_PORT,
}

# Set up random env vars
expected_get_env = dict[str, Any]()
expected_get_env[ApifyEnvVars.LOG_LEVEL.name.lower()] = 'INFO'
Expand Down Expand Up @@ -134,9 +115,7 @@ async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch)

# We need this override so that the actor doesn't fail when connecting to the platform events websocket
monkeypatch.delenv(ActorEnvVars.EVENTS_WEBSOCKET_URL)
monkeypatch.delenv(ApifyEnvVars.ACTOR_EVENTS_WS_URL)
expected_get_env[ActorEnvVars.EVENTS_WEBSOCKET_URL.name.lower()] = None
expected_get_env[ApifyEnvVars.ACTOR_EVENTS_WS_URL.name.lower()] = None

# Adjust expectations for timedelta fields
for env_name, env_value in expected_get_env.items():
Expand All @@ -148,10 +127,6 @@ async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch)
expected_get_env[ApifyEnvVars.DEDICATED_CPUS.name.lower()]
)

# Update expectations for legacy configuration
for old_name, new_name in legacy_env_vars.items():
expected_get_env[old_name.name.lower()] = expected_get_env[new_name.name.lower()]

await Actor.init()
assert Actor.get_env() == expected_get_env

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/actor/test_actor_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async def handler(websocket: websockets.asyncio.server.ServerConnection) -> None

async with websockets.asyncio.server.serve(handler, host='localhost') as ws_server:
port: int = ws_server.sockets[0].getsockname()[1] # type: ignore[index]
monkeypatch.setenv(ApifyEnvVars.ACTOR_EVENTS_WS_URL, f'ws://localhost:{port}')
monkeypatch.setenv(ActorEnvVars.EVENTS_WEBSOCKET_URL, f'ws://localhost:{port}')

mock_run_client = Mock()
mock_run_client.run.return_value.get = AsyncMock(
Expand Down
Loading
Loading