From 0bfde37742825cbc5768697ad486ca10ba68389f Mon Sep 17 00:00:00 2001 From: leohoare Date: Tue, 19 Nov 2024 08:51:27 +1100 Subject: [PATCH] get_client_async => get_async_client and typing for couritines --- openfeature/api.py | 2 +- openfeature/client.py | 26 ++++++++++++---------- openfeature/provider/__init__.py | 12 +++++----- openfeature/provider/in_memory_provider.py | 14 +++++++----- openfeature/provider/no_op_provider.py | 12 +++++----- tests/conftest.py | 2 +- tests/test_async_client.py | 6 ++--- 7 files changed, 41 insertions(+), 33 deletions(-) diff --git a/openfeature/api.py b/openfeature/api.py index f0acc3c7..82eee9ba 100644 --- a/openfeature/api.py +++ b/openfeature/api.py @@ -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) diff --git a/openfeature/client.py b/openfeature/client.py index 3168b2a2..e5416481 100644 --- a/openfeature/client.py +++ b/openfeature/client.py @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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. @@ -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. diff --git a/openfeature/provider/__init__.py b/openfeature/provider/__init__.py index e057418f..76c3cbae 100644 --- a/openfeature/provider/__init__.py +++ b/openfeature/provider/__init__.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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") diff --git a/openfeature/provider/in_memory_provider.py b/openfeature/provider/in_memory_provider.py index 7debd0d3..1280c66b 100644 --- a/openfeature/provider/in_memory_provider.py +++ b/openfeature/provider/in_memory_provider.py @@ -136,7 +136,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 self._resolve(flag_key, evaluation_context) async def resolve_string_details( @@ -144,7 +144,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 self._resolve(flag_key, evaluation_context) async def resolve_integer_details( @@ -152,7 +152,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 self._resolve(flag_key, evaluation_context) async def resolve_float_details( @@ -160,7 +160,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 self._resolve(flag_key, evaluation_context) async def resolve_object_details( @@ -168,5 +168,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]]: - return self._resolve(flag_key, evaluation_context) \ No newline at end of file + ) -> typing.Coroutine[ + typing.Any, typing.Any, FlagResolutionDetails[typing.Union[dict, list]] + ]: + return self._resolve(flag_key, evaluation_context) diff --git a/openfeature/provider/no_op_provider.py b/openfeature/provider/no_op_provider.py index a2bb5003..f451c3c1 100644 --- a/openfeature/provider/no_op_provider.py +++ b/openfeature/provider/no_op_provider.py @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/tests/conftest.py b/tests/conftest.py index b3dcc16a..d4755514 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_async_client.py b/tests/test_async_client.py index 49fc3355..25b16e86 100644 --- a/tests/test_async_client.py +++ b/tests/test_async_client.py @@ -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 @@ -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 ) @@ -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():