-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add async functionality to providers #413
base: main
Are you sure you want to change the base?
feat: Add async functionality to providers #413
Conversation
c003a56
to
4ec15be
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #413 +/- ##
==========================================
+ Coverage 97.55% 97.72% +0.16%
==========================================
Files 31 32 +1
Lines 1393 1629 +236
==========================================
+ Hits 1359 1592 +233
- Misses 34 37 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Hey @leohoare, this looks good so far. Could you please add tests covering async providers with sync client calls and vise versa? Thanks for your hard work on this. 🍻 |
86c64df
to
bb9a4e6
Compare
Thanks @beeme1mr, I've added some tests and addressed the coverage issues. One thing to note is sync methods are always enforced on async providers. Is this clear enough from the documentation? |
5142300
to
5d34cd8
Compare
Sorry, I didn't get a chance to look at this today. It's on my to-do for tomorrow. |
No rush, I'll be off grid over the weekend anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @leohoare, this looks good from what I can tell but I wouldn't consider myself a Python expert.
I see you have tests but would you mind also enumerating the expected behavior for the following scenarios?
- performing an async evaluation on a synonymous provider
- performing a sync evaluation on an async provider that implements the AbstractProvider
I believe I understand how everything will behave but I'd like to confirm.
Also, could someone with more Python experience please weigh in when you have a moment? FYI, @aepfli @guidobrei @federicobond @jamescarr @lukas-reining @toddbaert
Do you mean explain the scenarios or update the tests?
If async evaluation is not implemented, it will fall back to calling the synchronous function.
a provider that implements async calls is forced to implement sync functions.
If the provider chooses to only implement async functions and throw an error on the sync functions.
We're essentially offloading the decision to the provider on how to handle async/sync calls. Implementing the async calls is optional and defaults to sync when not implemented. |
3db8642
to
e51451d
Compare
Signed-off-by: leohoare <leo@insight.co>
Signed-off-by: leohoare <leo@insight.co>
Signed-off-by: leohoare <leo@insight.co>
… imports) Signed-off-by: leohoare <leo@insight.co>
Missed auto format Signed-off-by: Leo <37860104+leohoare@users.noreply.github.com> Signed-off-by: leohoare <leo@insight.co>
e51451d
to
72d69d5
Compare
Sorry keep forgetting to sign-off the commits -.- |
I've approved because I'm good with the approach. Hopefully others with more Python experience can also provide some thoughts. |
Sorry for the late reply, I have been on vacation. Will have a look in the next 1 or 2 days :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! I think we should mention the fallback mechanism in the documentation to avoid confusion. Users might expect asynchronous execution when calling those async functions, but they actually get synchronous function execution.
Signed-off-by: leohoare <leo@insight.co>
…terhooks and update readme with async doco Signed-off-by: leohoare <leo@insight.co>
I've updated the readme to include the suggestion @ChihweiLHBird, as well a general usage code block. @chrfwow due to the refactoring required in this PR, some of your recent merge had to be moved. |
Signed-off-by: leohoare <leo@insight.co>
Signed-off-by: leohoare <leo@insight.co>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes of my latest additions seem fine so far, but I have a few other questions
@@ -295,54 +461,224 @@ def evaluate_flag_details( # noqa: PLR0915 | |||
reversed_merged_hooks = merged_hooks[:] | |||
reversed_merged_hooks.reverse() | |||
|
|||
return provider, hook_context, hook_hints, merged_hooks, reversed_merged_hooks | |||
|
|||
def _assert_provider_status( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the function name _assert_provider_status
I would expect the function to throw an error when the provider is not ready. I would not expect the function to have any side effects like invoking hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about _validate_provider_status
? or do you have any other suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be possible to just throw the proper exception (both ProviderNotReadyError
and ProviderFatalError
are instances of OpenFeatureError
, so the exception handling should work)? This function is called inside the try catch block, so the error hooks will also be called this way.
If not, I would go for a name like _handle_provider_not_ready
, but I don't like that name either
default_value: typing.Any, | ||
evaluation_context: typing.Optional[EvaluationContext] = None, | ||
) -> FlagEvaluationDetails[typing.Any]: | ||
args = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a python expert. So purely out of interest, why do we need to box the args?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way it's used is just syntactic sugar to get around strict typing.
We call it like so:
args = ...
resolution = await get_details_callable(*args)
The alternative is passing in the arguments by key.
This option likely has a typing error as we're dynamically resolving the function call.
resolution = await get_details_callable(flag_key=flag_key,...)
Another option is arguments by order.
With this option, we lose the ability to have multiple defaults (unless you pass in arguments in the exact order) and order must match exactly, so it's not often recommended.
resolution = await get_details_callable(flag_key, default_value, evaluation_context)
When you pass it in like *args, it's a way of dynamically doing option 1.
This pattern was also copied from the sync function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is incorrect. passing *args
to get_details_callable()
unpacks them as positional arguments and therefore you can directly set them there. you are mistaken it with **kwargs
. Feel free to try it out and switch the position in the args
tuple and you will see how it messes up the function call.
Nevertheless I need to check, if it is possible to properly type the dynamic resolving, but not important in this PR, because it is done the same way in the sync variant as you mentioned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true, misinterpreted this sorry.
Happy to update this to **kwargs
or if you want to avoid scope creep, I can do this in a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nono, leave as is and feel free to give it a try in a separate PR, because the type checker won't be so nice to you
hook_hints, | ||
) | ||
|
||
def evaluate_flag_details( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that this function shares a lot of code with evaluate_flag_details
. Does the async prevent the extraction into a common function, or is there another reason for the duplication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the async prevent the extraction into a common function, or is there another reason for the duplication?
Somewhat yes, because the async call is in the middle of the try catch if we wanted to split it into common code we could only do something like:
- calls at the start of try in common call
error_code = self._assert_provider_status(
flag_type,
hook_context,
reversed_merged_hooks,
hook_hints,
)
if error_code:
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=error_code,
)
return flag_evaluation
merged_context = self._before_hooks_and_merge_context(
flag_type,
hook_context,
merged_hooks,
hook_hints,
evaluation_context,
)
- calls after the async call in common code
after_hooks(
flag_type,
hook_context,
flag_evaluation,
reversed_merged_hooks,
hook_hints,
)
return flag_evaluation
- A helper call for each of the exception
I didn't really deem this worth it and would lead to passing around a lot of variables to common functions.
What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave it as is 👍
default_value, | ||
evaluation_context, | ||
) | ||
get_details_callables_async: typing.Mapping[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't this map be global instead of a local variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, followed the pattern from the sync call but it could be global.
Edit: this would be pretty messy to do
- provider is dynamically evaluated each call
- functions from this provider are mapped when creating this dictionary
It means we'd need to push the logic into the AbstractProvider
init, which feels wrong.
If we wanted it to be statically called once, we'd have to require calling super() on provider implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a perfect use case for structural pattern matching, which we can use in 1-2 years, when the minimum supported Python version reaches 3.10 😅 but till then this is good enough.
@@ -402,6 +738,48 @@ def evaluate_flag_details( # noqa: PLR0915 | |||
hook_hints, | |||
) | |||
|
|||
async def _create_provider_evaluation_async( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function also shares most of its code with _create_provider_evaluation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, they need to be separate functions as this is needs to propagate the async function.
One big downside of python's handling of async is it often leads to forked/duplicated code.
One option is to convert the logic before/after the _get_details_callable for both functions into helper functions, however, they're already only a few calls to helpers.
I'll see what I can do to clean this up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see... Then let's leave it as is 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice work, just added a few comments
default_value: typing.Any, | ||
evaluation_context: typing.Optional[EvaluationContext] = None, | ||
) -> FlagEvaluationDetails[typing.Any]: | ||
args = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is incorrect. passing *args
to get_details_callable()
unpacks them as positional arguments and therefore you can directly set them there. you are mistaken it with **kwargs
. Feel free to try it out and switch the position in the args
tuple and you will see how it messes up the function call.
Nevertheless I need to check, if it is possible to properly type the dynamic resolving, but not important in this PR, because it is done the same way in the sync variant as you mentioned.
default_value, | ||
evaluation_context, | ||
) | ||
get_details_callables_async: typing.Mapping[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a perfect use case for structural pattern matching, which we can use in 1-2 years, when the minimum supported Python version reaches 3.10 😅 but till then this is good enough.
Greets everyone! I'd like to get this merged by the end of the week if possible. Please leave you feedback ASAP if you have any concerns. Thanks! @leohoare, thanks for you hard work and patience. It's important that we have consensus when making changes to the public APIs. So changes like this tend to take a while. |
This PR
Adds the ability for open feature providers to use async methods
It extends the single client and attempts to refactor some code
Related Issues
#284
#383
#385
Follow-up Tasks & TODOS