Skip to content

Commit

Permalink
fix: return correct type object in OpenFeatureClient._create_provider…
Browse files Browse the repository at this point in the history
…_evaluation (#136)

Signed-off-by: Federico Bond <federicobond@gmail.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
federicobond and toddbaert authored Jul 6, 2023
1 parent 291581f commit 052e149
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions open_feature/open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from open_feature.flag_evaluation.flag_evaluation_options import FlagEvaluationOptions
from open_feature.flag_evaluation.flag_type import FlagType
from open_feature.flag_evaluation.reason import Reason
from open_feature.flag_evaluation.resolution_details import FlagResolutionDetails
from open_feature.hooks.hook import Hook
from open_feature.hooks.hook_context import HookContext
from open_feature.hooks.hook_support import (
Expand All @@ -26,19 +27,20 @@

GetDetailCallable = typing.Union[
typing.Callable[
[str, bool, typing.Optional[EvaluationContext]], FlagEvaluationDetails[bool]
[str, bool, typing.Optional[EvaluationContext]], FlagResolutionDetails[bool]
],
typing.Callable[
[str, int, typing.Optional[EvaluationContext]], FlagEvaluationDetails[int]
[str, int, typing.Optional[EvaluationContext]], FlagResolutionDetails[int]
],
typing.Callable[
[str, float, typing.Optional[EvaluationContext]], FlagEvaluationDetails[float]
[str, float, typing.Optional[EvaluationContext]], FlagResolutionDetails[float]
],
typing.Callable[
[str, str, typing.Optional[EvaluationContext]], FlagEvaluationDetails[str]
[str, str, typing.Optional[EvaluationContext]], FlagResolutionDetails[str]
],
typing.Callable[
[str, dict, typing.Optional[EvaluationContext]], FlagEvaluationDetails[dict]
[str, typing.Union[dict, list], typing.Optional[EvaluationContext]],
FlagResolutionDetails[typing.Union[dict, list]],
],
]

Expand Down Expand Up @@ -356,13 +358,20 @@ def _create_provider_evaluation(
if not get_details_callable:
raise GeneralError(error_message="Unknown flag type")

value = get_details_callable(*args)
resolution = get_details_callable(*args)

# we need to check the get_args to be compatible with union types.
is_right_instance = isinstance(
value.value, typing.get_args(flag_type.value)
) or isinstance(value.value, flag_type.value)
resolution.value, typing.get_args(flag_type.value)
) or isinstance(resolution.value, flag_type.value)
if not is_right_instance:
raise TypeMismatchError()

return value
return FlagEvaluationDetails(
flag_key=flag_key,
value=resolution.value,
variant=resolution.variant,
reason=resolution.reason,
error_code=resolution.error_code,
error_message=resolution.error_message,
)

0 comments on commit 052e149

Please sign in to comment.