Skip to content

Commit

Permalink
fix: use str values in FlagType enum for easier typing (#137)
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Bond <federicobond@gmail.com>
  • Loading branch information
federicobond authored Jul 7, 2023
1 parent 052e149 commit 292a0df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
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)}")

0 comments on commit 292a0df

Please sign in to comment.