Skip to content

Commit 31afa64

Browse files
authored
chore: improve resolve details callable type hints (#449)
improve resolve details callable type hints Signed-off-by: gruebel <[email protected]>
1 parent f29c450 commit 31afa64

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

openfeature/client.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import typing
3+
from collections.abc import Awaitable
34
from dataclasses import dataclass
45

56
from openfeature import _event_support, api
@@ -37,46 +38,6 @@
3738

3839
logger = logging.getLogger("openfeature")
3940

40-
GetDetailCallable = typing.Union[
41-
typing.Callable[
42-
[str, bool, typing.Optional[EvaluationContext]], FlagResolutionDetails[bool]
43-
],
44-
typing.Callable[
45-
[str, int, typing.Optional[EvaluationContext]], FlagResolutionDetails[int]
46-
],
47-
typing.Callable[
48-
[str, float, typing.Optional[EvaluationContext]], FlagResolutionDetails[float]
49-
],
50-
typing.Callable[
51-
[str, str, typing.Optional[EvaluationContext]], FlagResolutionDetails[str]
52-
],
53-
typing.Callable[
54-
[str, typing.Union[dict, list], typing.Optional[EvaluationContext]],
55-
FlagResolutionDetails[typing.Union[dict, list]],
56-
],
57-
]
58-
GetDetailCallableAsync = typing.Union[
59-
typing.Callable[
60-
[str, bool, typing.Optional[EvaluationContext]],
61-
typing.Awaitable[FlagResolutionDetails[bool]],
62-
],
63-
typing.Callable[
64-
[str, int, typing.Optional[EvaluationContext]],
65-
typing.Awaitable[FlagResolutionDetails[int]],
66-
],
67-
typing.Callable[
68-
[str, float, typing.Optional[EvaluationContext]],
69-
typing.Awaitable[FlagResolutionDetails[float]],
70-
],
71-
typing.Callable[
72-
[str, str, typing.Optional[EvaluationContext]],
73-
typing.Awaitable[FlagResolutionDetails[str]],
74-
],
75-
typing.Callable[
76-
[str, typing.Union[dict, list], typing.Optional[EvaluationContext]],
77-
typing.Awaitable[FlagResolutionDetails[typing.Union[dict, list]]],
78-
],
79-
]
8041
TypeMap = dict[
8142
FlagType,
8243
typing.Union[
@@ -88,6 +49,26 @@
8849
],
8950
]
9051

52+
T = typing.TypeVar("T", bool, int, float, str, typing.Union[dict, list])
53+
54+
55+
class ResolveDetailsCallable(typing.Protocol[T]):
56+
def __call__(
57+
self,
58+
flag_key: str,
59+
default_value: T,
60+
evaluation_context: typing.Optional[EvaluationContext],
61+
) -> FlagResolutionDetails[T]: ...
62+
63+
64+
class ResolveDetailsCallableAsync(typing.Protocol[T]):
65+
def __call__(
66+
self,
67+
flag_key: str,
68+
default_value: T,
69+
evaluation_context: typing.Optional[EvaluationContext],
70+
) -> Awaitable[FlagResolutionDetails[T]]: ...
71+
9172

9273
@dataclass
9374
class ClientMetadata:
@@ -702,7 +683,7 @@ async def _create_provider_evaluation_async(
702683
evaluation_context: typing.Optional[EvaluationContext] = None,
703684
) -> FlagEvaluationDetails[typing.Any]:
704685
get_details_callables_async: typing.Mapping[
705-
FlagType, GetDetailCallableAsync
686+
FlagType, ResolveDetailsCallableAsync
706687
] = {
707688
FlagType.BOOLEAN: provider.resolve_boolean_details_async,
708689
FlagType.INTEGER: provider.resolve_integer_details_async,
@@ -714,7 +695,7 @@ async def _create_provider_evaluation_async(
714695
if not get_details_callable:
715696
raise GeneralError(error_message="Unknown flag type")
716697

717-
resolution = await get_details_callable( # type: ignore[call-arg]
698+
resolution = await get_details_callable(
718699
flag_key=flag_key,
719700
default_value=default_value,
720701
evaluation_context=evaluation_context,
@@ -752,7 +733,7 @@ def _create_provider_evaluation(
752733
:return: a FlagEvaluationDetails object with the fully evaluated flag from a
753734
provider
754735
"""
755-
get_details_callables: typing.Mapping[FlagType, GetDetailCallable] = {
736+
get_details_callables: typing.Mapping[FlagType, ResolveDetailsCallable] = {
756737
FlagType.BOOLEAN: provider.resolve_boolean_details,
757738
FlagType.INTEGER: provider.resolve_integer_details,
758739
FlagType.FLOAT: provider.resolve_float_details,
@@ -764,7 +745,7 @@ def _create_provider_evaluation(
764745
if not get_details_callable:
765746
raise GeneralError(error_message="Unknown flag type")
766747

767-
resolution = get_details_callable( # type: ignore[call-arg]
748+
resolution = get_details_callable(
768749
flag_key=flag_key,
769750
default_value=default_value,
770751
evaluation_context=evaluation_context,

0 commit comments

Comments
 (0)