Skip to content

Commit

Permalink
fixup! Add evaluation details to finally hook stage open-feature#403
Browse files Browse the repository at this point in the history
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
  • Loading branch information
chrfwow committed Jan 28, 2025
1 parent d0d74cf commit e803ae6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
8 changes: 7 additions & 1 deletion openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,13 @@ def evaluate_flag_details( # noqa: PLR0915
return flag_evaluation

finally:
after_all_hooks(flag_type, hook_context, flag_evaluation, reversed_merged_hooks, hook_hints)
after_all_hooks(
flag_type,
hook_context,
flag_evaluation,
reversed_merged_hooks,
hook_hints
)

def _create_provider_evaluation(
self,
Expand Down
7 changes: 6 additions & 1 deletion openfeature/hook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ def error(
"""
pass

def finally_after(self, hook_context: HookContext, details: FlagEvaluationDetails[typing.Any], hints: HookHints) -> None:
def finally_after(
self,
hook_context: HookContext,
details: FlagEvaluationDetails[typing.Any],
hints: HookHints
) -> None:
"""
Run after flag evaluation, including any error processing.
This will always run. Errors will be swallowed.
Expand Down
22 changes: 11 additions & 11 deletions tests/features/steps/hooks_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from openfeature.hook import Hook


@when("a hook is added to the client")
def step_impl(context):
@when('a hook is added to the client')
def step_impl_add_hook(context):
hook = MagicMock(spec=Hook)
hook.before = MagicMock()
hook.after = MagicMock()
Expand All @@ -18,34 +18,34 @@ def step_impl(context):


@then('error hooks should be called')
def step_impl(context):
def step_impl_call_error(context):
assert context.hook.before.called
assert context.hook.error.called
assert context.hook.finally_after.called


@then('non-error hooks should be called')
def step_impl(context):
def step_impl_call_non_error(context):
assert context.hook.before.called
assert context.hook.after.called
assert context.hook.finally_after.called


def get_hook_from_name(context, hook_name):
if hook_name.lower() == "before":
if hook_name.lower() == 'before':
return context.hook.before
elif hook_name.lower() == "after":
elif hook_name.lower() == 'after':
return context.hook.after
elif hook_name.lower() == "error":
elif hook_name.lower() == 'error':
return context.hook.error
elif hook_name.lower() == "finally_after" or hook_name.lower() == "finally after":
elif hook_name.lower() == 'finally':
return context.hook.finally_after
else:
raise ValueError(str(hook_name) + " is not a valid hook name")
raise ValueError(str(hook_name) + ' is not a valid hook name')


def convert_value_from_flag_type(value, flag_type):
if value == "None":
if value == 'None':
return None
if flag_type.lower() == 'boolean':
return bool(value)
Expand All @@ -56,7 +56,7 @@ def convert_value_from_flag_type(value, flag_type):
return value

@then('"{hook_names}" hooks should have evaluation details')
def step_impl(context, hook_names):
def step_impl_should_have_eval_details(context, hook_names):
for hook_name in hook_names.split(', '):
hook = get_hook_from_name(context, hook_name)
for row in context.table:
Expand Down
10 changes: 5 additions & 5 deletions tests/hook/test_hook_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ def test_after_hooks_run_after_method(mock_hook):

def test_finally_after_hooks_run_finally_after_method(mock_hook):
# Given
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, "")
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, '')
flag_evaluation_details = FlagEvaluationDetails(
hook_context.flag_key, "val", "unknown"
)
hook_hints = MappingProxyType({})
# When
after_all_hooks(FlagType.BOOLEAN, hook_context, flag_evaluation_details, [mock_hook], hook_hints)
after_all_hooks(
FlagType.BOOLEAN, hook_context, flag_evaluation_details, [mock_hook], hook_hints
)
# Then
mock_hook.supports_flag_value_type.assert_called_once()
mock_hook.finally_after.assert_called_once()
mock_hook.finally_after.assert_called_with(
hook_context=hook_context,
details=flag_evaluation_details,
hints=hook_hints
hook_context=hook_context, details=flag_evaluation_details, hints=hook_hints
)

0 comments on commit e803ae6

Please sign in to comment.