Skip to content

Commit 853af23

Browse files
committed
Fix type check error
1 parent 4bb1121 commit 853af23

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

optimizely/decision_service.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +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]]
86+
holdout: Optional[Dict[str, str]] = None
8787

8888

8989
class DecisionService:
@@ -712,7 +712,8 @@ def get_decision_for_flag(
712712
holdout_decision = self.get_variation_for_holdout(holdout, user_context, project_config)
713713
reasons.extend(holdout_decision['reasons'])
714714

715-
if not holdout_decision['decision']:
715+
decision = holdout_decision['decision']
716+
if (decision.experiment is None and decision.variation is None and decision.holdout is None):
716717
continue
717718

718719
message = (
@@ -738,7 +739,7 @@ def get_decision_for_flag(
738739
reasons.extend(fallback_result['reasons'])
739740

740741
return {
741-
'decision': fallback_result.get('decision'),
742+
'decision': fallback_result['decision'],
742743
'error': fallback_result.get('error', False),
743744
'reasons': reasons
744745
}
@@ -772,7 +773,7 @@ def get_variation_for_holdout(
772773
self.logger.info(message)
773774
decide_reasons.append(message)
774775
return {
775-
'decision': None,
776+
'decision': Decision(None, None, None, None, None),
776777
'error': False,
777778
'reasons': decide_reasons
778779
}
@@ -793,17 +794,22 @@ def get_variation_for_holdout(
793794
decide_reasons.extend(reasons_received)
794795

795796
if not user_meets_audience_conditions:
796-
message = f"User '{user_id}' does not meet the conditions for holdout '{holdout['key']}'."
797+
message = (
798+
f"User '{user_id}' does not meet the conditions for holdout "
799+
f"'{holdout['key']}'."
800+
)
797801
self.logger.debug(message)
798802
decide_reasons.append(message)
799803
return {
800-
'decision': None,
804+
'decision': Decision(None, None, None, None, None),
801805
'error': False,
802806
'reasons': decide_reasons
803807
}
804808

805809
# Bucket user into holdout variation
806-
variation, bucket_reasons = self.bucketer.bucket(project_config, holdout, user_id, bucketing_id)
810+
variation, bucket_reasons = self.bucketer.bucket(
811+
project_config, holdout, user_id, bucketing_id # type: ignore[arg-type]
812+
)
807813
decide_reasons.extend(bucket_reasons)
808814

809815
if variation:
@@ -834,7 +840,7 @@ def get_variation_for_holdout(
834840
self.logger.info(message)
835841
decide_reasons.append(message)
836842
return {
837-
'decision': None,
843+
'decision': Decision(None, None, None, None, None),
838844
'error': False,
839845
'reasons': decide_reasons
840846
}

tests/test_decision_service_holdout.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ def test_user_bucketed_into_holdout_returns_before_experiments(self):
289289
# Decision should be valid
290290
if decision_result.get('decision'):
291291
decision = decision_result['decision']
292-
self.assertIsNotNone(decision.experiment)
293-
self.assertIsNotNone(decision.source)
292+
self.assertEqual(decision.source, enums.DecisionSources.HOLDOUT)
293+
self.assertIsNotNone(decision.holdout)
294+
self.assertEqual(decision.holdout.get('key'), 'test_holdout')
294295

295296
def test_no_holdout_decision_falls_through_to_experiment_and_rollout(self):
296297
"""When holdout returns no decision, should fall through to experiment and rollout evaluation."""

0 commit comments

Comments
 (0)