Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions open_feature/flag_evaluation/flag_type.py
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 16 additions & 5 deletions open_feature/open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)}")