Skip to content

Commit 029262d

Browse files
update: refine traffic allocation type hints and key naming in bucketer and decision service
1 parent ec19c3b commit 029262d

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

optimizely/bucketer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def bucket_to_entity_id(
169169
self,
170170
bucketing_id: str,
171171
experiment: Experiment,
172-
traffic_allocations: list,
172+
traffic_allocations: list[TrafficAllocation],
173173
group: Optional[Group] = None
174174
) -> tuple[Optional[str], list[str]]:
175175
"""
@@ -193,8 +193,8 @@ def bucket_to_entity_id(
193193

194194
matched = False
195195
for allocation in group.trafficAllocation:
196-
end_of_range = cast(int, allocation.get("end_of_range", 0))
197-
entity_id = cast(Optional[str], allocation.get("entity_id"))
196+
end_of_range = allocation['endOfRange']
197+
entity_id = allocation['entityId']
198198
if bucket_val < end_of_range:
199199
matched = True
200200
if entity_id != experiment.id:
@@ -218,8 +218,8 @@ def bucket_to_entity_id(
218218
decide_reasons.append(f'Generated experiment bucket value {bucket_val} for key "{bucket_key}".')
219219

220220
for allocation in traffic_allocations:
221-
end_of_range = allocation.get("end_of_range", 0)
222-
entity_id = allocation.get("entity_id")
221+
end_of_range = allocation['endOfRange']
222+
entity_id = allocation['entityId']
223223
if bucket_val < end_of_range:
224224
decide_reasons.append(
225225
f'User bucketed into entity id "{entity_id}".'

optimizely/decision_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# prevent circular dependenacy by skipping import at runtime
3131
from .project_config import ProjectConfig
3232
from .logger import Logger
33+
from .helpers.types import TrafficAllocation
3334

3435

3536
class CmabDecisionResult(TypedDict):
@@ -391,9 +392,9 @@ def get_variation(
391392
if experiment.cmab:
392393
CMAB_DUMMY_ENTITY_ID = "$"
393394
# Build the CMAB-specific traffic allocation
394-
cmab_traffic_allocation = [{
395-
"entity_id": CMAB_DUMMY_ENTITY_ID,
396-
"end_of_range": experiment.cmab['trafficAllocation']
395+
cmab_traffic_allocation: list[TrafficAllocation] = [{
396+
"entityId": CMAB_DUMMY_ENTITY_ID,
397+
"endOfRange": experiment.cmab['trafficAllocation']
397398
}]
398399

399400
# Check if user is in CMAB traffic allocation

0 commit comments

Comments
 (0)