Skip to content

Commit

Permalink
get_client_async => get_async_client and typing for couritines
Browse files Browse the repository at this point in the history
  • Loading branch information
leohoare committed Nov 18, 2024
1 parent 657ef96 commit 0bfde37
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion openfeature/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_client(
return OpenFeatureClient(domain=domain, version=version)


def get_client_async(
def get_async_client(
domain: typing.Optional[str] = None, version: typing.Optional[str] = None
) -> AsyncOpenFeatureClient:
return AsyncOpenFeatureClient(domain=domain, version=version)
Expand Down
26 changes: 14 additions & 12 deletions openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ async def get_boolean_value(
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> bool:
) -> typing.Coroutine[typing.Any, typing.Any, bool]:
details = await self.get_boolean_details(
flag_key,
default_value,
Expand All @@ -488,7 +488,7 @@ async def get_boolean_details(
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[bool]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[bool]]:
return await self.evaluate_flag_details(
FlagType.BOOLEAN,
flag_key,
Expand All @@ -503,7 +503,7 @@ async def get_string_value(
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> str:
) -> typing.Coroutine[typing.Any, typing.Any, str]:
details = await self.get_string_details(
flag_key,
default_value,
Expand All @@ -518,7 +518,7 @@ async def get_string_details(
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[str]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[str]]:
return await self.evaluate_flag_details(
FlagType.STRING,
flag_key,
Expand All @@ -533,7 +533,7 @@ async def get_integer_value(
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> int:
) -> typing.Coroutine[typing.Any, typing.Any, int]:
details = await self.get_integer_details(
flag_key,
default_value,
Expand All @@ -548,7 +548,7 @@ async def get_integer_details(
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[int]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[int]]:
return await self.evaluate_flag_details(
FlagType.INTEGER,
flag_key,
Expand All @@ -563,7 +563,7 @@ async def get_float_value(
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> float:
) -> typing.Coroutine[typing.Any, typing.Any, float]:
details = await self.get_float_details(
flag_key,
default_value,
Expand All @@ -578,7 +578,7 @@ async def get_float_details(
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[float]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[float]]:
return await self.evaluate_flag_details(
FlagType.FLOAT,
flag_key,
Expand All @@ -593,7 +593,7 @@ async def get_object_value(
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> typing.Union[dict, list]:
) -> typing.Coroutine[typing.Any, typing.Any, typing.Union[dict, list]]:
details = await self.get_object_details(
flag_key,
default_value,
Expand All @@ -608,7 +608,9 @@ async def get_object_details(
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[typing.Union[dict, list]]:
) -> typing.Coroutine[
typing.Any, typing.Any, FlagEvaluationDetails[typing.Union[dict, list]]
]:
return await self.evaluate_flag_details(
FlagType.OBJECT,
flag_key,
Expand All @@ -624,7 +626,7 @@ async def evaluate_flag_details( # noqa: PLR0915
default_value: typing.Any,
evaluation_context: typing.Optional[EvaluationContext] = None,
flag_evaluation_options: typing.Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails[typing.Any]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[typing.Any]]:
"""
Evaluate the flag requested by the user from the clients provider.
Expand Down Expand Up @@ -770,7 +772,7 @@ async def _create_provider_evaluation(
flag_key: str,
default_value: typing.Any,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagEvaluationDetails[typing.Any]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagEvaluationDetails[typing.Any]]:
"""
Asynchronous encapsulated method to create a FlagEvaluationDetail from a specific provider.
Expand Down
12 changes: 7 additions & 5 deletions openfeature/provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async def resolve_boolean_details(
flag_key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[bool]]:
raise NotImplementedError("Method not implemented")

@abstractmethod
Expand All @@ -182,7 +182,7 @@ async def resolve_string_details(
flag_key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[str]]:
raise NotImplementedError("Method not implemented")

@abstractmethod
Expand All @@ -191,7 +191,7 @@ async def resolve_integer_details(
flag_key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[int]]:
raise NotImplementedError("Method not implemented")

@abstractmethod
Expand All @@ -200,7 +200,7 @@ async def resolve_float_details(
flag_key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[float]]:
raise NotImplementedError("Method not implemented")

@abstractmethod
Expand All @@ -209,5 +209,7 @@ async def resolve_object_details(
flag_key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]:
) -> typing.Coroutine[
typing.Any, typing.Any, FlagResolutionDetails[typing.Union[dict, list]]
]:
raise NotImplementedError("Method not implemented")
14 changes: 8 additions & 6 deletions openfeature/provider/in_memory_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,39 @@ async def resolve_boolean_details(
flag_key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[bool]]:
return self._resolve(flag_key, evaluation_context)

async def resolve_string_details(
self,
flag_key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[str]]:
return self._resolve(flag_key, evaluation_context)

async def resolve_integer_details(
self,
flag_key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[int]]:
return self._resolve(flag_key, evaluation_context)

async def resolve_float_details(
self,
flag_key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[float]]:
return self._resolve(flag_key, evaluation_context)

async def resolve_object_details(
self,
flag_key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]:
return self._resolve(flag_key, evaluation_context)
) -> typing.Coroutine[
typing.Any, typing.Any, FlagResolutionDetails[typing.Union[dict, list]]
]:
return self._resolve(flag_key, evaluation_context)
12 changes: 7 additions & 5 deletions openfeature/provider/no_op_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def resolve_boolean_details(
flag_key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[bool]]:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DEFAULT,
Expand All @@ -98,7 +98,7 @@ async def resolve_string_details(
flag_key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[str]]:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DEFAULT,
Expand All @@ -110,7 +110,7 @@ async def resolve_integer_details(
flag_key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[int]]:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DEFAULT,
Expand All @@ -122,7 +122,7 @@ async def resolve_float_details(
flag_key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]:
) -> typing.Coroutine[typing.Any, typing.Any, FlagResolutionDetails[float]]:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DEFAULT,
Expand All @@ -134,7 +134,9 @@ async def resolve_object_details(
flag_key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]:
) -> typing.Coroutine[
typing.Any, typing.Any, FlagResolutionDetails[typing.Union[dict, list]]
]:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DEFAULT,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def no_op_provider_client():
@pytest.fixture()
def no_op_provider_client_async():
api.set_provider(AsyncNoOpProvider())
return api.get_client_async("my-async-client")
return api.get_async_client("my-async-client")
6 changes: 3 additions & 3 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from openfeature.api import add_hooks, get_client_async, set_provider
from openfeature.api import add_hooks, get_async_client, set_provider
from openfeature.client import AsyncOpenFeatureClient
from openfeature.event import ProviderEvent, ProviderEventDetails
from openfeature.exception import ErrorCode, OpenFeatureError
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_add_remove_event_handler_async():
set_provider(provider)
spy = MagicMock()

client = get_client_async()
client = get_async_client()
client.add_handler(
ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, spy.provider_configuration_changed
)
Expand All @@ -109,7 +109,7 @@ def handler(*args, **kwargs):

for _ in range(10):
time.sleep(0.01)
client = get_client_async(str(uuid.uuid4()))
client = get_async_client(str(uuid.uuid4()))
client.add_handler(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, handler)

def emit_events_task():
Expand Down

0 comments on commit 0bfde37

Please sign in to comment.