Skip to content

Commit 1610d0d

Browse files
authored
fix: Treat default alias as default storage when in schema (#827)
Treat `default` alias as default storage when in schema. To be aligned with the new JS implementation and match the platform users' expectations.
1 parent 2282f13 commit 1610d0d

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/apify/storage_clients/_apify/_alias_resolving.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def open_by_alias(
7676
The alias mapping is stored in the default key-value store for persistence across Actor runs.
7777
7878
Args:
79-
alias: The alias name for the storage (e.g., '__default__', 'my-storage').
79+
alias: The alias name for the storage (e.g., 'my-storage').
8080
storage_type: The type of storage to open.
8181
collection_client: The Apify API collection client for the storage type.
8282
get_resource_client_by_id: A callable that takes a storage ID and returns the resource client.
@@ -131,6 +131,8 @@ class AliasResolver:
131131
_alias_init_lock: Lock | None = None
132132
"""Lock for creating alias storages. Only one alias storage can be created at the time. Global for all instances."""
133133

134+
default_storage_key: ClassVar[str] = '__default__'
135+
134136
def __init__(
135137
self,
136138
storage_type: Literal['Dataset', 'KeyValueStore', 'RequestQueue'],
@@ -195,7 +197,7 @@ async def resolve_id(self) -> str | None:
195197
"""
196198
# First try to find the alias in the configuration mapping to avoid any API calls.
197199
# This mapping is maintained by the Apify platform and does not have to be maintained in the default KVS.
198-
if self._configuration.actor_storages and self._alias != 'default':
200+
if self._configuration.actor_storages:
199201
storage_maps = {
200202
'Dataset': self._configuration.actor_storages['datasets'],
201203
'KeyValueStore': self._configuration.actor_storages['key_value_stores'],

src/apify/storage_clients/_apify/_api_client_creation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from apify_client import ApifyClientAsync
66
from crawlee._utils.crypto import crypto_random_object_id
77

8-
from apify.storage_clients._apify._alias_resolving import open_by_alias
8+
from apify.storage_clients._apify._alias_resolving import AliasResolver, open_by_alias
99

1010
if TYPE_CHECKING:
1111
from apify_client.clients import DatasetClientAsync, KeyValueStoreClientAsync, RequestQueueClientAsync
@@ -113,7 +113,7 @@ def get_resource_client(storage_id: str) -> DatasetClientAsync:
113113
# Normalize unnamed default storage to unnamed storage aliased as `__default__`.
114114
case (None, None, None, None):
115115
return await open_by_alias(
116-
alias='__default__',
116+
alias=AliasResolver.default_storage_key,
117117
storage_type=storage_type,
118118
collection_client=collection_client,
119119
get_resource_client_by_id=get_resource_client,

tests/e2e/test_schema_storages/actor_source/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ async def main() -> None:
77
dataset = await Actor.open_dataset(alias='custom')
88
expected_id = Actor.configuration.actor_storages['datasets']['custom']
99
assert dataset.id == expected_id
10+
11+
implicit_default_dataset = await Actor.open_dataset()
12+
explicit_default_dataset = await Actor.open_dataset(alias='default')
13+
assert implicit_default_dataset.id == explicit_default_dataset.id

tests/integration/test_storages.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from apify import Actor, Configuration
1111
from apify.storage_clients import ApifyStorageClient, MemoryStorageClient, SmartApifyStorageClient
12+
from apify.storage_clients._apify._alias_resolving import AliasResolver
1213

1314

1415
@pytest.mark.parametrize(
@@ -125,3 +126,11 @@ async def test_actor_implicit_storage_init(apify_token: str) -> None:
125126
assert await Actor.open_dataset() is not await Actor.open_dataset(force_cloud=True)
126127
assert await Actor.open_key_value_store() is not await Actor.open_key_value_store(force_cloud=True)
127128
assert await Actor.open_request_queue() is not await Actor.open_request_queue(force_cloud=True)
129+
130+
131+
async def test_default_storage(apify_token: str) -> None:
132+
service_locator.set_configuration(Configuration(token=apify_token))
133+
async with Actor:
134+
dataset_1 = await Actor.open_dataset(force_cloud=True)
135+
dataset_2 = await Actor.open_dataset(force_cloud=True, alias=AliasResolver.default_storage_key)
136+
assert dataset_1.id == dataset_2.id

0 commit comments

Comments
 (0)