Skip to content

Commit 980f556

Browse files
vdusekclaude
andcommitted
Clean up _consts.py: remove unused ActorEventTypes, move env var classification lists to test file, inline BASE64_REGEXP
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e5d6e9c commit 980f556

3 files changed

Lines changed: 85 additions & 119 deletions

File tree

‎src/apify/_consts.py‎

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@
33
import re
44
from datetime import timedelta
55
from enum import Enum, StrEnum
6-
from typing import Literal, get_args
76

87
EVENT_LISTENERS_TIMEOUT = timedelta(seconds=5)
8+
"""Timeout for waiting on event listeners to finish during Actor exit."""
99

10-
BASE64_REGEXP = '[-A-Za-z0-9+/]*={0,3}'
1110
ENCRYPTED_STRING_VALUE_PREFIX = 'ENCRYPTED_VALUE'
11+
"""Prefix for encrypted string values in Actor input."""
12+
1213
ENCRYPTED_JSON_VALUE_PREFIX = 'ENCRYPTED_JSON'
14+
"""Prefix for encrypted JSON values in Actor input."""
15+
1316
ENCRYPTED_INPUT_VALUE_REGEXP = re.compile(
14-
f'^({ENCRYPTED_STRING_VALUE_PREFIX}|{ENCRYPTED_JSON_VALUE_PREFIX}):(?:({BASE64_REGEXP}):)?({BASE64_REGEXP}):({BASE64_REGEXP})$'
17+
r'^(ENCRYPTED_VALUE|ENCRYPTED_JSON):(?:([-A-Za-z0-9+/]*={0,3}):)?([-A-Za-z0-9+/]*={0,3}):([-A-Za-z0-9+/]*={0,3})$'
1518
)
16-
17-
18-
class ActorEventTypes(StrEnum):
19-
"""Event types that can be sent to Actors during execution."""
20-
21-
SYSTEM_INFO = 'systemInfo'
22-
MIGRATING = 'migrating'
23-
PERSIST_STATE = 'persistState'
24-
ABORTING = 'aborting'
19+
"""Regex matching encrypted input values with base64-encoded components."""
2520

2621

2722
class ActorEnvVars(StrEnum):
@@ -91,88 +86,3 @@ class ActorExitCodes(int, Enum):
9186

9287
SUCCESS = 0
9388
ERROR_USER_FUNCTION_THREW = 91
94-
95-
96-
# Environment variable type classification lists.
97-
98-
INTEGER_ENV_VARS_TYPE = Literal[
99-
ActorEnvVars.MAX_PAID_DATASET_ITEMS,
100-
ActorEnvVars.MEMORY_MBYTES,
101-
ActorEnvVars.STANDBY_PORT,
102-
ActorEnvVars.WEB_SERVER_PORT,
103-
ApifyEnvVars.DEDICATED_CPUS,
104-
ApifyEnvVars.LOG_LEVEL,
105-
ApifyEnvVars.METAMORPH_AFTER_SLEEP_MILLIS,
106-
ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS,
107-
ApifyEnvVars.PROXY_PORT,
108-
ApifyEnvVars.SYSTEM_INFO_INTERVAL_MILLIS,
109-
]
110-
111-
INTEGER_ENV_VARS: list[INTEGER_ENV_VARS_TYPE] = list(get_args(INTEGER_ENV_VARS_TYPE))
112-
113-
FLOAT_ENV_VARS_TYPE = Literal[
114-
ActorEnvVars.MAX_TOTAL_CHARGE_USD,
115-
ApifyEnvVars.MAX_USED_CPU_RATIO,
116-
]
117-
118-
FLOAT_ENV_VARS: list[FLOAT_ENV_VARS_TYPE] = list(get_args(FLOAT_ENV_VARS_TYPE))
119-
120-
BOOL_ENV_VARS_TYPE = Literal[
121-
ApifyEnvVars.DISABLE_BROWSER_SANDBOX,
122-
ApifyEnvVars.DISABLE_OUTDATED_WARNING,
123-
ApifyEnvVars.HEADLESS,
124-
ApifyEnvVars.IS_AT_HOME,
125-
ApifyEnvVars.PERSIST_STORAGE,
126-
ApifyEnvVars.PURGE_ON_START,
127-
ApifyEnvVars.USER_IS_PAYING,
128-
]
129-
130-
BOOL_ENV_VARS: list[BOOL_ENV_VARS_TYPE] = list(get_args(BOOL_ENV_VARS_TYPE))
131-
132-
DATETIME_ENV_VARS_TYPE = Literal[
133-
ActorEnvVars.STARTED_AT,
134-
ActorEnvVars.TIMEOUT_AT,
135-
]
136-
137-
DATETIME_ENV_VARS: list[DATETIME_ENV_VARS_TYPE] = list(get_args(DATETIME_ENV_VARS_TYPE))
138-
139-
STRING_ENV_VARS_TYPE = Literal[
140-
ActorEnvVars.BUILD_ID,
141-
ActorEnvVars.BUILD_NUMBER,
142-
ActorEnvVars.DEFAULT_DATASET_ID,
143-
ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID,
144-
ActorEnvVars.DEFAULT_REQUEST_QUEUE_ID,
145-
ActorEnvVars.EVENTS_WEBSOCKET_URL,
146-
ActorEnvVars.FULL_NAME,
147-
ActorEnvVars.ID,
148-
ActorEnvVars.INPUT_KEY,
149-
ActorEnvVars.PERMISSION_LEVEL,
150-
ActorEnvVars.RUN_ID,
151-
ActorEnvVars.STANDBY_URL,
152-
ActorEnvVars.TASK_ID,
153-
ActorEnvVars.WEB_SERVER_URL,
154-
ApifyEnvVars.API_BASE_URL,
155-
ApifyEnvVars.API_PUBLIC_BASE_URL,
156-
ApifyEnvVars.DEFAULT_BROWSER_PATH,
157-
ApifyEnvVars.FACT,
158-
ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_FILE,
159-
ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_PASSPHRASE,
160-
ApifyEnvVars.LOCAL_STORAGE_DIR,
161-
ApifyEnvVars.LOG_FORMAT,
162-
ApifyEnvVars.META_ORIGIN,
163-
ApifyEnvVars.PROXY_HOSTNAME,
164-
ApifyEnvVars.PROXY_PASSWORD,
165-
ApifyEnvVars.PROXY_STATUS_URL,
166-
ApifyEnvVars.SDK_LATEST_VERSION,
167-
ApifyEnvVars.TOKEN,
168-
ApifyEnvVars.USER_ID,
169-
ApifyEnvVars.WORKFLOW_KEY,
170-
]
171-
172-
STRING_ENV_VARS: list[STRING_ENV_VARS_TYPE] = list(get_args(STRING_ENV_VARS_TYPE))
173-
174-
COMMA_SEPARATED_LIST_ENV_VARS_TYPE = Literal[ActorEnvVars.BUILD_TAGS]
175-
176-
COMMA_SEPARATED_LIST_ENV_VARS: list[COMMA_SEPARATED_LIST_ENV_VARS_TYPE] = list(
177-
get_args(COMMA_SEPARATED_LIST_ENV_VARS_TYPE)
178-
)

‎tests/e2e/test_actor_events.py‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import TYPE_CHECKING
55

66
from apify import Actor
7-
from apify._consts import ActorEventTypes
87

98
if TYPE_CHECKING:
109
from .conftest import MakeActorFunction, RunActorFunction
@@ -23,28 +22,28 @@ async def main() -> None:
2322

2423
from crawlee.events._types import Event, EventSystemInfoData
2524

26-
from apify._consts import ActorEventTypes, ApifyEnvVars
25+
from apify._consts import ApifyEnvVars
2726

2827
os.environ[ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS] = '900'
2928

3029
was_system_info_emitted = False
3130
system_infos = list[EventSystemInfoData]()
3231

33-
def on_event(event_type: ActorEventTypes) -> Callable:
32+
def on_event(event_type: str) -> Callable:
3433
async def log_event(data: Any) -> None:
3534
nonlocal was_system_info_emitted
3635
nonlocal system_infos
3736
print(f'Got actor event ({event_type=}, {data=})')
3837
await Actor.push_data({'event_type': event_type, 'data': data})
39-
if event_type == ActorEventTypes.SYSTEM_INFO:
38+
if event_type == 'systemInfo':
4039
was_system_info_emitted = True
4140
system_infos.append(data)
4241

4342
return log_event
4443

4544
async with Actor:
46-
Actor.on(Event.SYSTEM_INFO, on_event(ActorEventTypes.SYSTEM_INFO))
47-
Actor.on(Event.PERSIST_STATE, on_event(ActorEventTypes.PERSIST_STATE))
45+
Actor.on(Event.SYSTEM_INFO, on_event('systemInfo'))
46+
Actor.on(Event.PERSIST_STATE, on_event('persistState'))
4847
await asyncio.sleep(3)
4948

5049
# The SYSTEM_INFO event sometimes takes a while to appear, let's wait for it for a while longer.
@@ -63,12 +62,8 @@ async def log_event(data: Any) -> None:
6362
assert run_result.status.value == 'SUCCEEDED'
6463

6564
dataset_items_page = await actor.last_run().dataset().list_items()
66-
persist_state_events = [
67-
item for item in dataset_items_page.items if item['event_type'] == ActorEventTypes.PERSIST_STATE
68-
]
69-
system_info_events = [
70-
item for item in dataset_items_page.items if item['event_type'] == ActorEventTypes.SYSTEM_INFO
71-
]
65+
persist_state_events = [item for item in dataset_items_page.items if item['event_type'] == 'persistState']
66+
system_info_events = [item for item in dataset_items_page.items if item['event_type'] == 'systemInfo']
7267
assert len(persist_state_events) > 2
7368
assert len(system_info_events) > 0
7469

‎tests/unit/actor/test_actor_env_helpers.py‎

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,77 @@
99
from pydantic_core import TzInfo
1010

1111
from apify import Actor
12-
from apify._consts import (
13-
BOOL_ENV_VARS,
14-
COMMA_SEPARATED_LIST_ENV_VARS,
15-
DATETIME_ENV_VARS,
16-
FLOAT_ENV_VARS,
17-
INTEGER_ENV_VARS,
18-
STRING_ENV_VARS,
19-
ActorEnvVars,
20-
ApifyEnvVars,
21-
)
12+
from apify._consts import ActorEnvVars, ApifyEnvVars
13+
14+
INTEGER_ENV_VARS: list[ActorEnvVars | ApifyEnvVars] = [
15+
ActorEnvVars.MAX_PAID_DATASET_ITEMS,
16+
ActorEnvVars.MEMORY_MBYTES,
17+
ActorEnvVars.STANDBY_PORT,
18+
ActorEnvVars.WEB_SERVER_PORT,
19+
ApifyEnvVars.DEDICATED_CPUS,
20+
ApifyEnvVars.LOG_LEVEL,
21+
ApifyEnvVars.METAMORPH_AFTER_SLEEP_MILLIS,
22+
ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS,
23+
ApifyEnvVars.PROXY_PORT,
24+
ApifyEnvVars.SYSTEM_INFO_INTERVAL_MILLIS,
25+
]
26+
27+
FLOAT_ENV_VARS: list[ActorEnvVars | ApifyEnvVars] = [
28+
ActorEnvVars.MAX_TOTAL_CHARGE_USD,
29+
ApifyEnvVars.MAX_USED_CPU_RATIO,
30+
]
31+
32+
BOOL_ENV_VARS: list[ApifyEnvVars] = [
33+
ApifyEnvVars.DISABLE_BROWSER_SANDBOX,
34+
ApifyEnvVars.DISABLE_OUTDATED_WARNING,
35+
ApifyEnvVars.HEADLESS,
36+
ApifyEnvVars.IS_AT_HOME,
37+
ApifyEnvVars.PERSIST_STORAGE,
38+
ApifyEnvVars.PURGE_ON_START,
39+
ApifyEnvVars.USER_IS_PAYING,
40+
]
41+
42+
DATETIME_ENV_VARS: list[ActorEnvVars] = [
43+
ActorEnvVars.STARTED_AT,
44+
ActorEnvVars.TIMEOUT_AT,
45+
]
46+
47+
STRING_ENV_VARS: list[ActorEnvVars | ApifyEnvVars] = [
48+
ActorEnvVars.BUILD_ID,
49+
ActorEnvVars.BUILD_NUMBER,
50+
ActorEnvVars.DEFAULT_DATASET_ID,
51+
ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID,
52+
ActorEnvVars.DEFAULT_REQUEST_QUEUE_ID,
53+
ActorEnvVars.EVENTS_WEBSOCKET_URL,
54+
ActorEnvVars.FULL_NAME,
55+
ActorEnvVars.ID,
56+
ActorEnvVars.INPUT_KEY,
57+
ActorEnvVars.PERMISSION_LEVEL,
58+
ActorEnvVars.RUN_ID,
59+
ActorEnvVars.STANDBY_URL,
60+
ActorEnvVars.TASK_ID,
61+
ActorEnvVars.WEB_SERVER_URL,
62+
ApifyEnvVars.API_BASE_URL,
63+
ApifyEnvVars.API_PUBLIC_BASE_URL,
64+
ApifyEnvVars.DEFAULT_BROWSER_PATH,
65+
ApifyEnvVars.FACT,
66+
ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_FILE,
67+
ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_PASSPHRASE,
68+
ApifyEnvVars.LOCAL_STORAGE_DIR,
69+
ApifyEnvVars.LOG_FORMAT,
70+
ApifyEnvVars.META_ORIGIN,
71+
ApifyEnvVars.PROXY_HOSTNAME,
72+
ApifyEnvVars.PROXY_PASSWORD,
73+
ApifyEnvVars.PROXY_STATUS_URL,
74+
ApifyEnvVars.SDK_LATEST_VERSION,
75+
ApifyEnvVars.TOKEN,
76+
ApifyEnvVars.USER_ID,
77+
ApifyEnvVars.WORKFLOW_KEY,
78+
]
79+
80+
COMMA_SEPARATED_LIST_ENV_VARS: list[ActorEnvVars] = [
81+
ActorEnvVars.BUILD_TAGS,
82+
]
2283

2384
if TYPE_CHECKING:
2485
from pathlib import Path

0 commit comments

Comments
 (0)