@@ -22,19 +22,6 @@ def error_hooks(
2222 )
2323
2424
25- async def error_hooks_async (
26- flag_type : FlagType ,
27- hook_context : HookContext ,
28- exception : Exception ,
29- hooks : typing .List [Hook ],
30- hints : typing .Optional [HookHints ] = None ,
31- ) -> None :
32- kwargs = {"hook_context" : hook_context , "exception" : exception , "hints" : hints }
33- await _execute_hooks_async (
34- flag_type = flag_type , hooks = hooks , hook_method = HookType .ERROR , ** kwargs
35- )
36-
37-
3825def after_all_hooks (
3926 flag_type : FlagType ,
4027 hook_context : HookContext ,
@@ -47,18 +34,6 @@ def after_all_hooks(
4734 )
4835
4936
50- async def after_all_hooks_async (
51- flag_type : FlagType ,
52- hook_context : HookContext ,
53- hooks : typing .List [Hook ],
54- hints : typing .Optional [HookHints ] = None ,
55- ) -> None :
56- kwargs = {"hook_context" : hook_context , "hints" : hints }
57- await _execute_hooks_async (
58- flag_type = flag_type , hooks = hooks , hook_method = HookType .FINALLY_AFTER , ** kwargs
59- )
60-
61-
6237def after_hooks (
6338 flag_type : FlagType ,
6439 hook_context : HookContext ,
@@ -72,19 +47,6 @@ def after_hooks(
7247 )
7348
7449
75- async def after_hooks_async (
76- flag_type : FlagType ,
77- hook_context : HookContext ,
78- details : FlagEvaluationDetails [typing .Any ],
79- hooks : typing .List [Hook ],
80- hints : typing .Optional [HookHints ] = None ,
81- ) -> None :
82- kwargs = {"hook_context" : hook_context , "details" : details , "hints" : hints }
83- await _execute_hooks_async_unchecked (
84- flag_type = flag_type , hooks = hooks , hook_method = HookType .AFTER , ** kwargs
85- )
86-
87-
8850def before_hooks (
8951 flag_type : FlagType ,
9052 hook_context : HookContext ,
@@ -103,23 +65,6 @@ def before_hooks(
10365 return EvaluationContext ()
10466
10567
106- async def before_hooks_async (
107- flag_type : FlagType ,
108- hook_context : HookContext ,
109- hooks : typing .List [Hook ],
110- hints : typing .Optional [HookHints ] = None ,
111- ) -> EvaluationContext :
112- kwargs = {"hook_context" : hook_context , "hints" : hints }
113- executed_hooks = await _execute_hooks_async (
114- flag_type = flag_type , hooks = hooks , hook_method = HookType .BEFORE , ** kwargs
115- )
116- filtered_hooks = [result for result in executed_hooks if result is not None ]
117- if filtered_hooks :
118- return reduce (lambda a , b : a .merge (b ), filtered_hooks )
119-
120- return EvaluationContext ()
121-
122-
12368def _execute_hooks (
12469 flag_type : FlagType ,
12570 hooks : typing .List [Hook ],
@@ -143,29 +88,6 @@ def _execute_hooks(
14388 ]
14489
14590
146- async def _execute_hooks_async (
147- flag_type : FlagType ,
148- hooks : typing .List [Hook ],
149- hook_method : HookType ,
150- ** kwargs : typing .Any ,
151- ) -> list :
152- """
153- Run multiple hooks of any hook type. All of these hooks will be run through an
154- exception check.
155-
156- :param flag_type: particular type of flag
157- :param hooks: a list of hooks
158- :param hook_method: the type of hook that is being run
159- :param kwargs: arguments that need to be provided to the hook method
160- :return: a list of results from the applied hook methods
161- """
162- return [
163- await _execute_hook_checked_async (hook , hook_method , ** kwargs )
164- for hook in hooks
165- if hook .supports_flag_value_type (flag_type )
166- ]
167-
168-
16991def _execute_hooks_unchecked (
17092 flag_type : FlagType ,
17193 hooks : typing .List [Hook ],
@@ -190,30 +112,6 @@ def _execute_hooks_unchecked(
190112 ]
191113
192114
193- async def _execute_hooks_async_unchecked (
194- flag_type : FlagType ,
195- hooks : typing .List [Hook ],
196- hook_method : HookType ,
197- ** kwargs : typing .Any ,
198- ) -> typing .List [typing .Optional [EvaluationContext ]]:
199- """
200- Execute a single hook without checking whether an exception is thrown. This is
201- used in the before and after hooks since any exception will be caught in the
202- client.
203-
204- :param flag_type: particular type of flag
205- :param hooks: a list of hooks
206- :param hook_method: the type of hook that is being run
207- :param kwargs: arguments that need to be provided to the hook method
208- :return: a list of results from the applied hook methods
209- """
210- return [
211- await getattr (hook , hook_method .value )(** kwargs )
212- for hook in hooks
213- if hook .supports_flag_value_type (flag_type )
214- ]
215-
216-
217115def _execute_hook_checked (
218116 hook : Hook , hook_method : HookType , ** kwargs : typing .Any
219117) -> typing .Optional [EvaluationContext ]:
@@ -234,25 +132,3 @@ def _execute_hook_checked(
234132 except Exception : # pragma: no cover
235133 logger .exception (f"Exception when running { hook_method .value } hooks" )
236134 return None
237-
238-
239- async def _execute_hook_checked_async (
240- hook : Hook , hook_method : HookType , ** kwargs : typing .Any
241- ) -> typing .Optional [EvaluationContext ]:
242- """
243- Try and run a single hook and catch any exception thrown. This is used in the
244- after all and error hooks since any error thrown at this point needs to be caught.
245-
246- :param hook: a list of hooks
247- :param hook_method: the type of hook that is being run
248- :param kwargs: arguments that need to be provided to the hook method
249- :return: the result of the hook method
250- """
251- try :
252- return typing .cast (
253- "typing.Optional[EvaluationContext]" ,
254- await getattr (hook , hook_method .value )(** kwargs ),
255- )
256- except Exception : # pragma: no cover
257- logger .exception (f"Exception when running { hook_method .value } hooks" )
258- return None
0 commit comments