diff --git a/open_feature/flag_evaluation/flag_type.py b/open_feature/flag_evaluation/flag_type.py index f19d1d2d..5408edf6 100644 --- a/open_feature/flag_evaluation/flag_type.py +++ b/open_feature/flag_evaluation/flag_type.py @@ -1,10 +1,9 @@ -import typing from enum import Enum class FlagType(Enum): - BOOLEAN = bool - STRING = str - OBJECT = typing.Union[dict, list] - FLOAT = float - INTEGER = int + BOOLEAN = "BOOLEAN" + STRING = "STRING" + OBJECT = "OBJECT" + FLOAT = "FLOAT" + INTEGER = "INTEGER" diff --git a/open_feature/open_feature_client.py b/open_feature/open_feature_client.py index 92652cf5..53e81031 100644 --- a/open_feature/open_feature_client.py +++ b/open_feature/open_feature_client.py @@ -361,11 +361,7 @@ def _create_provider_evaluation( resolution = get_details_callable(*args) # we need to check the get_args to be compatible with union types. - is_right_instance = isinstance( - resolution.value, typing.get_args(flag_type.value) - ) or isinstance(resolution.value, flag_type.value) - if not is_right_instance: - raise TypeMismatchError() + _typecheck_flag_value(resolution.value, flag_type) return FlagEvaluationDetails( flag_key=flag_key, @@ -375,3 +371,18 @@ def _create_provider_evaluation( error_code=resolution.error_code, error_message=resolution.error_message, ) + + +def _typecheck_flag_value(value, flag_type): + type_map = { + FlagType.BOOLEAN: bool, + FlagType.STRING: str, + FlagType.OBJECT: typing.Union[dict, list], + FlagType.FLOAT: float, + FlagType.INTEGER: int, + } + _type = type_map.get(flag_type) + if not _type: + raise GeneralError(error_message="Unknown flag type") + if not isinstance(value, _type): + raise TypeMismatchError(f"Expected type {_type} but got {type(value)}")