2
2
Call loop machinery
3
3
"""
4
4
import sys
5
+ from typing import cast , Generator , List , Mapping , Sequence , Union
6
+ from typing import TYPE_CHECKING
5
7
6
8
from ._result import HookCallError , _Result , _raise_wrapfail
7
9
10
+ if TYPE_CHECKING :
11
+ from ._hooks import HookImpl
8
12
9
- def _multicall (hook_name , hook_impls , caller_kwargs , firstresult ):
13
+
14
+ def _multicall (
15
+ hook_name : str ,
16
+ hook_impls : Sequence ["HookImpl" ],
17
+ caller_kwargs : Mapping [str , object ],
18
+ firstresult : bool ,
19
+ ) -> Union [object , List [object ]]:
10
20
"""Execute a call into multiple python functions/methods and return the
11
21
result(s).
12
22
13
23
``caller_kwargs`` comes from _HookCaller.__call__().
14
24
"""
15
25
__tracebackhide__ = True
16
- results = []
26
+ results : List [ object ] = []
17
27
excinfo = None
18
28
try : # run impl and wrapper setup functions in a loop
19
29
teardowns = []
@@ -30,7 +40,10 @@ def _multicall(hook_name, hook_impls, caller_kwargs, firstresult):
30
40
31
41
if hook_impl .hookwrapper :
32
42
try :
33
- gen = hook_impl .function (* args )
43
+ # If this cast is not valid, a type error is raised below,
44
+ # which is the desired response.
45
+ res = hook_impl .function (* args )
46
+ gen = cast (Generator [None , _Result [object ], None ], res )
34
47
next (gen ) # first yield
35
48
teardowns .append (gen )
36
49
except StopIteration :
@@ -42,10 +55,16 @@ def _multicall(hook_name, hook_impls, caller_kwargs, firstresult):
42
55
if firstresult : # halt further impl calls
43
56
break
44
57
except BaseException :
45
- excinfo = sys .exc_info ()
58
+ _excinfo = sys .exc_info ()
59
+ assert _excinfo [0 ] is not None
60
+ assert _excinfo [1 ] is not None
61
+ assert _excinfo [2 ] is not None
62
+ excinfo = (_excinfo [0 ], _excinfo [1 ], _excinfo [2 ])
46
63
finally :
47
64
if firstresult : # first result hooks return a single value
48
- outcome = _Result (results [0 ] if results else None , excinfo )
65
+ outcome : _Result [Union [object , List [object ]]] = _Result (
66
+ results [0 ] if results else None , excinfo
67
+ )
49
68
else :
50
69
outcome = _Result (results , excinfo )
51
70
0 commit comments