1515
1616from typing import TYPE_CHECKING , Any , Optional , Union
1717
18- from optimizely .helpers .types import HoldoutDict , VariationDict
18+ from optimizely .helpers .types import VariationDict
1919
2020
2121from . import decision_service
@@ -261,25 +261,6 @@ def _get_feature_enabled(self, variation: Optional[Union[entities.Variation, Var
261261 except (AttributeError , KeyError , TypeError ):
262262 return False
263263
264- def _get_experiment_key (self , experiment : Optional [Union [entities .Experiment , HoldoutDict ]]) -> Optional [str ]:
265- """Helper to extract experiment/holdout key from either dict or Experiment object.
266- Args:
267- experiment: Either a HoldoutDict (from holdout) or entities.Experiment object
268- Returns:
269- The experiment/holdout key as a string, or None if not available
270- """
271- if experiment is None :
272- return None
273-
274- try :
275- if isinstance (experiment , dict ):
276- return experiment .get ('key' )
277- else :
278- return experiment .key
279- except (AttributeError , KeyError , TypeError ):
280- self .logger .warning (f"Unable to extract experiment key from { type (experiment )} " )
281- return None
282-
283264 def _validate_instantiation_options (self ) -> None :
284265 """ Helper method to validate all instantiation parameters.
285266
@@ -452,7 +433,7 @@ def _get_feature_variable_for_type(
452433 decision_result = self .decision_service .get_variation_for_feature (project_config , feature_flag , user_context )
453434 decision = decision_result ['decision' ]
454435
455- if decision and decision .variation :
436+ if decision .variation :
456437
457438 feature_enabled = self ._get_feature_enabled (decision .variation )
458439 if feature_enabled :
@@ -474,7 +455,7 @@ def _get_feature_variable_for_type(
474455
475456 if decision .source in (enums .DecisionSources .FEATURE_TEST , enums .DecisionSources .HOLDOUT ):
476457 source_info = {
477- 'experiment_key' : self . _get_experiment_key ( decision .experiment ) ,
458+ 'experiment_key' : decision . experiment . key if decision .experiment else None ,
478459 'variation_key' : self ._get_variation_key (decision .variation ),
479460 }
480461
@@ -578,7 +559,7 @@ def _get_all_feature_variables_for_type(
578559
579560 if decision .source == enums .DecisionSources .FEATURE_TEST :
580561 source_info = {
581- 'experiment_key' : self . _get_experiment_key ( decision .experiment ) ,
562+ 'experiment_key' : decision . experiment . key if decision .experiment else None ,
582563 'variation_key' : self ._get_variation_key (decision .variation ),
583564 }
584565
@@ -811,33 +792,30 @@ def is_feature_enabled(self, feature_key: str, user_id: str, attributes: Optiona
811792 user_context = OptimizelyUserContext (self , self .logger , user_id , attributes , False )
812793
813794 decision = self .decision_service .get_variation_for_feature (project_config , feature , user_context )['decision' ]
814- cmab_uuid = decision .cmab_uuid if decision else None
795+ cmab_uuid = decision .cmab_uuid
815796
816- is_source_experiment = decision .source == enums .DecisionSources .FEATURE_TEST if decision else False
817- is_source_rollout = decision .source == enums .DecisionSources .ROLLOUT if decision else False
797+ is_source_experiment = decision .source == enums .DecisionSources .FEATURE_TEST
798+ is_source_rollout = decision .source == enums .DecisionSources .ROLLOUT
818799
819- if decision and decision .variation :
800+ if decision .variation :
820801 if self ._get_feature_enabled (decision .variation ) is True :
821802 feature_enabled = True
822803
823- if (decision and (is_source_rollout or not decision .variation ) and
824- project_config .get_send_flag_decisions_value ()):
804+ if (is_source_rollout or not decision .variation ) and project_config .get_send_flag_decisions_value ():
825805 self ._send_impression_event (
826- project_config , decision .experiment , decision .variation , feature .key ,
827- self ._get_experiment_key (decision .experiment ) or '' , str (decision .source ), feature_enabled ,
828- user_id , attributes , cmab_uuid
806+ project_config , decision .experiment , decision .variation , feature .key , decision .experiment .key if
807+ decision .experiment else '' , str (decision .source ), feature_enabled , user_id , attributes , cmab_uuid
829808 )
830809
831810 # Send event if Decision came from an experiment.
832- if decision and is_source_experiment and decision .variation and decision .experiment :
811+ if is_source_experiment and decision .variation and decision .experiment :
833812 source_info = {
834- 'experiment_key' : self . _get_experiment_key ( decision .experiment ) ,
813+ 'experiment_key' : decision .experiment . key ,
835814 'variation_key' : self ._get_variation_key (decision .variation ),
836815 }
837816 self ._send_impression_event (
838- project_config , decision .experiment , decision .variation , feature .key ,
839- self ._get_experiment_key (decision .experiment ) or '' , str (decision .source ), feature_enabled ,
840- user_id , attributes , cmab_uuid
817+ project_config , decision .experiment , decision .variation , feature .key , decision .experiment .key ,
818+ str (decision .source ), feature_enabled , user_id , attributes , cmab_uuid
841819 )
842820
843821 if feature_enabled :
@@ -1266,17 +1244,17 @@ def _create_optimizely_decision(
12661244 ) -> OptimizelyDecision :
12671245 user_id = user_context .user_id
12681246 feature_enabled = False
1269- if flag_decision and flag_decision .variation is not None :
1247+ if flag_decision .variation is not None :
12701248 if self ._get_feature_enabled (flag_decision .variation ):
12711249 feature_enabled = True
12721250
12731251 self .logger .info (f'Feature { flag_key } is enabled for user { user_id } { feature_enabled } "' )
12741252
12751253 # Create Optimizely Decision Result.
12761254 attributes = user_context .get_user_attributes ()
1277- rule_key = self . _get_experiment_key ( flag_decision .experiment ) if flag_decision else None
1255+ rule_key = flag_decision .experiment . key if flag_decision . experiment else None
12781256 all_variables = {}
1279- decision_source = flag_decision .source if flag_decision else None
1257+ decision_source = flag_decision .source
12801258 decision_event_dispatched = False
12811259
12821260 feature_flag = project_config .feature_key_map .get (flag_key )
0 commit comments