Skip to content

Commit 4bb1121

Browse files
committed
Fix type issues
1 parent 8c6e39d commit 4bb1121

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

optimizely/bucketer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,13 @@ def bucket_to_entity_id(
221221
decide_reasons.append(message)
222222

223223
if has_cmab:
224-
traffic_allocations = [
225-
{
226-
"entityId": "$",
227-
"endOfRange": experiment.cmab['trafficAllocation']
228-
}
229-
]
224+
if experiment.cmab:
225+
traffic_allocations = [
226+
{
227+
"entityId": "$",
228+
"endOfRange": experiment.cmab['trafficAllocation']
229+
}
230+
]
230231

231232
# Bucket user if not in white-list and in group (if any)
232233
variation_id = self.find_bucket(project_config, bucketing_id,

optimizely/decision_service.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313

1414
from __future__ import annotations
15-
from typing import TYPE_CHECKING, NamedTuple, Optional, Sequence, List, TypedDict
15+
from typing import TYPE_CHECKING, NamedTuple, Optional, Sequence, List, TypedDict, Dict
1616

1717
from . import bucketer
1818
from . import entities
@@ -83,6 +83,7 @@ class Decision(NamedTuple):
8383
variation: Optional[entities.Variation]
8484
source: Optional[str]
8585
cmab_uuid: Optional[str]
86+
holdout: Optional[Dict[str, str]]
8687

8788

8889
class DecisionService:
@@ -744,7 +745,7 @@ def get_decision_for_flag(
744745

745746
def get_variation_for_holdout(
746747
self,
747-
holdout: dict[str, str],
748+
holdout: Dict[str, str],
748749
user_context: OptimizelyUserContext,
749750
project_config: ProjectConfig
750751
) -> DecisionResult:
@@ -806,14 +807,23 @@ def get_variation_for_holdout(
806807
decide_reasons.extend(bucket_reasons)
807808

808809
if variation:
810+
# For holdouts, variation is a dict, not a Variation entity
811+
variation_key = variation['key'] if isinstance(variation, dict) else variation.key
809812
message = (
810-
f"The user '{user_id}' is bucketed into variation '{variation['key']}' "
813+
f"The user '{user_id}' is bucketed into variation '{variation_key}' "
811814
f"of holdout '{holdout['key']}'."
812815
)
813816
self.logger.info(message)
814817
decide_reasons.append(message)
815818

816-
holdout_decision = Decision(holdout, variation, enums.DecisionSources.HOLDOUT, None)
819+
# Create Decision with holdout - experiment is None, holdout field contains the holdout dict
820+
holdout_decision: Decision = Decision(
821+
experiment=None,
822+
variation=None,
823+
source=enums.DecisionSources.HOLDOUT,
824+
cmab_uuid=None,
825+
holdout=holdout
826+
)
817827
return {
818828
'decision': holdout_decision,
819829
'error': False,

optimizely/project_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
262262
variations = holdout.get('variations')
263263
if variations:
264264
for variation in variations:
265-
variation_key = variation.get('key')
266-
variation_id = variation.get('id')
265+
variation_key = variation.get('key') if isinstance(variation, dict) else None
266+
variation_id = variation.get('id') if isinstance(variation, dict) else None
267267
if variation_key and variation_id:
268268
self.variation_key_map[holdout_key][variation_key] = variation
269269
self.variation_id_map[holdout_key][variation_id] = variation

0 commit comments

Comments
 (0)