Skip to content

Commit a7306a8

Browse files
Pijukatelvdusek
andauthored
Apply suggestions from code review
Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
1 parent d15fadb commit a7306a8

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/apify/_configuration.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,32 @@ def _transform_to_list(value: Any) -> list[str] | None:
3535

3636

3737
class ActorStorages(TypedDict):
38+
"""Mapping of storage aliases to their IDs, grouped by storage type.
39+
40+
Populated from the `ACTOR_STORAGES_JSON` env var that the Apify platform sets when an Actor declares
41+
named storages in its `actor.json` schema. Each key maps a user-defined alias (e.g. `'custom'`)
42+
to the platform-assigned storage ID.
43+
"""
44+
3845
key_value_stores: dict[str, str]
3946
datasets: dict[str, str]
4047
request_queues: dict[str, str]
4148

4249

43-
def _load_storage_keys(
44-
data: None | str | ActorStorages,
45-
) -> ActorStorages | None:
46-
"""Load storage keys."""
50+
def _load_storage_keys(data: None | str | ActorStorages) -> ActorStorages | None:
51+
"""Parse the `ACTOR_STORAGES_JSON` value into a normalized `ActorStorages` dict.
52+
53+
The platform provides this as a JSON string with camelCase keys (`keyValueStores`, `requestQueues`, `datasets`).
54+
This validator deserializes the JSON when needed and normalizes the keys to snake_case, falling back to empty
55+
dicts for missing storage types.
56+
57+
Args:
58+
data: Raw value - `None` when the env var is unset, a JSON string from the env var, or an already-parsed
59+
`ActorStorages` dict when set programmatically.
60+
61+
Returns:
62+
Normalized storage mapping, or `None` if the input is `None`.
63+
"""
4764
if data is None:
4865
return None
4966
storage_mapping = json.loads(data) if isinstance(data, str) else data
@@ -470,7 +487,7 @@ class Configuration(CrawleeConfiguration):
470487
ActorStorages | None,
471488
Field(
472489
alias='actor_storages_json',
473-
description='Storage IDs for the actor',
490+
description='Mapping of storage aliases to their platform-assigned IDs.',
474491
),
475492
BeforeValidator(_load_storage_keys),
476493
] = None

tests/e2e/test_schema_storages/actor_source/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
async def main() -> None:
55
async with Actor:
66
assert Actor.configuration.actor_storages
7-
assert (await Actor.open_dataset(alias='custom')).id == Actor.configuration.actor_storages['datasets']['custom']
7+
dataset = await Actor.open_dataset(alias='custom')
8+
expected_id = Actor.configuration.actor_storages['datasets']['custom']
9+
assert dataset.id == expected_id

0 commit comments

Comments
 (0)