Skip to content

Commit 7bf2dbd

Browse files
committed
chore: Fix data type annotation in hook definition (#289)
1 parent 655a087 commit 7bf2dbd

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

contract-tests/hook.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ldclient.hook import Hook, EvaluationSeriesContext
22
from ldclient.evaluation import EvaluationDetail
33

4-
from typing import Any, Optional
4+
from typing import Optional
55
import requests
66

77

@@ -12,13 +12,13 @@ def __init__(self, name: str, callback: str, data: dict, errors: dict):
1212
self.__data = data
1313
self.__errors = errors
1414

15-
def before_evaluation(self, series_context: EvaluationSeriesContext, data: dict) -> Any:
15+
def before_evaluation(self, series_context: EvaluationSeriesContext, data: dict) -> dict:
1616
return self.__post("beforeEvaluation", series_context, data, None)
1717

18-
def after_evaluation(self, series_context: EvaluationSeriesContext, data: Any, detail: EvaluationDetail) -> Any:
18+
def after_evaluation(self, series_context: EvaluationSeriesContext, data: dict, detail: EvaluationDetail) -> dict:
1919
return self.__post("afterEvaluation", series_context, data, detail)
2020

21-
def __post(self, stage: str, series_context: EvaluationSeriesContext, data: Any, detail: Optional[EvaluationDetail]) -> Any:
21+
def __post(self, stage: str, series_context: EvaluationSeriesContext, data: dict, detail: Optional[EvaluationDetail]) -> dict:
2222
if stage in self.__errors:
2323
raise Exception(self.__errors[stage])
2424

@@ -42,4 +42,4 @@ def __post(self, stage: str, series_context: EvaluationSeriesContext, data: Any,
4242

4343
requests.post(self.__callback, json=payload)
4444

45-
return {**(data or {}), **self.__data.get(stage, {})}
45+
return {**data, **self.__data.get(stage, {})}

ldclient/client.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
from ldclient.migrations import Stage, OpTracker
3838
from ldclient.impl.flag_tracker import FlagTrackerImpl
3939

40-
from threading import Lock
41-
42-
43-
4440

4541
class _FeatureStoreClientWrapper(FeatureStore):
4642
"""Provides additional behavior that the client requires before or after feature store operations.
@@ -639,24 +635,24 @@ def __evaluate_with_hooks(self, key: str, context: Context, default_value: Any,
639635

640636
return evaluation_result
641637

642-
def __execute_before_evaluation(self, hooks: List[Hook], series_context: EvaluationSeriesContext) -> List[Any]:
638+
def __execute_before_evaluation(self, hooks: List[Hook], series_context: EvaluationSeriesContext) -> List[dict]:
643639
return [
644640
self.__try_execute_stage("beforeEvaluation", hook.metadata.name, lambda: hook.before_evaluation(series_context, {}))
645641
for hook in hooks
646642
]
647643

648-
def __execute_after_evaluation(self, hooks: List[Hook], series_context: EvaluationSeriesContext, hook_data: List[Any], evaluation_detail: EvaluationDetail) -> List[Any]:
644+
def __execute_after_evaluation(self, hooks: List[Hook], series_context: EvaluationSeriesContext, hook_data: List[dict], evaluation_detail: EvaluationDetail) -> List[dict]:
649645
return [
650646
self.__try_execute_stage("afterEvaluation", hook.metadata.name, lambda: hook.after_evaluation(series_context, data, evaluation_detail))
651647
for (hook, data) in reversed(list(zip(hooks, hook_data)))
652648
]
653649

654-
def __try_execute_stage(self, method: str, hook_name: str, block: Callable[[], Any]) -> Any:
650+
def __try_execute_stage(self, method: str, hook_name: str, block: Callable[[], dict]) -> dict:
655651
try:
656652
return block()
657653
except BaseException as e:
658654
log.error(f"An error occurred in {method} of the hook {hook_name}: #{e}")
659-
return None
655+
return {}
660656

661657
@property
662658
def big_segment_store_status_provider(self) -> BigSegmentStoreStatusProvider:

ldclient/hook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def metadata(self) -> Metadata:
4848
return Metadata(name='UNDEFINED')
4949

5050
@abstractmethod
51-
def before_evaluation(self, series_context: EvaluationSeriesContext, data: Any) -> Any:
51+
def before_evaluation(self, series_context: EvaluationSeriesContext, data: dict) -> dict:
5252
"""
5353
The before method is called during the execution of a variation method
5454
before the flag value has been determined. The method is executed
@@ -63,7 +63,7 @@ def before_evaluation(self, series_context: EvaluationSeriesContext, data: Any)
6363
return data
6464

6565
@abstractmethod
66-
def after_evaluation(self, series_context: EvaluationSeriesContext, data: Any, detail: EvaluationDetail) -> dict:
66+
def after_evaluation(self, series_context: EvaluationSeriesContext, data: dict, detail: EvaluationDetail) -> dict:
6767
"""
6868
The after method is called during the execution of the variation method
6969
after the flag value has been determined. The method is executed

ldclient/testing/test_ldclient_hooks.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from ldclient import LDClient, Config, Context
33
from ldclient.hook import Hook, Metadata, EvaluationSeriesContext
44
from ldclient.migrations import Stage
5-
65
from ldclient.integrations.test_data import TestData
76

8-
from typing import Callable, Any
7+
from typing import Callable
98

109

1110
def record(label, log):
@@ -16,18 +15,18 @@ def inner(*args, **kwargs):
1615

1716

1817
class MockHook(Hook):
19-
def __init__(self, before_evaluation: Callable[[EvaluationSeriesContext, Any], dict], after_evaluation: Callable[[EvaluationSeriesContext, Any, EvaluationDetail], dict]):
18+
def __init__(self, before_evaluation: Callable[[EvaluationSeriesContext, dict], dict], after_evaluation: Callable[[EvaluationSeriesContext, dict, EvaluationDetail], dict]):
2019
self.__before_evaluation = before_evaluation
2120
self.__after_evaluation = after_evaluation
2221

2322
@property
2423
def metadata(self) -> Metadata:
2524
return Metadata(name='test-hook')
2625

27-
def before_evaluation(self, series_context: EvaluationSeriesContext, data):
26+
def before_evaluation(self, series_context: EvaluationSeriesContext, data: dict) -> dict:
2827
return self.__before_evaluation(series_context, data)
2928

30-
def after_evaluation(self, series_context: EvaluationSeriesContext, data, detail: EvaluationDetail):
29+
def after_evaluation(self, series_context: EvaluationSeriesContext, data: dict, detail: EvaluationDetail) -> dict:
3130
return self.__after_evaluation(series_context, data, detail)
3231

3332

@@ -95,7 +94,7 @@ def test_passing_data_from_before_to_after():
9594
assert calls[0] == "from before"
9695

9796

98-
def test_exception_in_before_passes_none():
97+
def test_exception_in_before_passes_empty_dict():
9998
def raise_exception(series_context, data):
10099
raise Exception("error")
101100

@@ -107,7 +106,7 @@ def raise_exception(series_context, data):
107106
client.variation('flag-key', user, False)
108107

109108
assert len(calls) == 1
110-
assert calls[0] is None
109+
assert calls[0] == {}
111110

112111

113112
def test_exceptions_do_not_affect_data_passing_order():
@@ -127,7 +126,7 @@ def raise_exception(series_context, data):
127126
# NOTE: These are reversed since the push happens in the after_evaluation
128127
# (when hooks are reversed)
129128
assert calls[0] == "third hook"
130-
assert calls[1] is None
129+
assert calls[1] == {}
131130
assert calls[2] == "first hook"
132131

133132

0 commit comments

Comments
 (0)