15
15
import asyncio
16
16
import traceback
17
17
from types import TracebackType
18
- from typing import Any , Awaitable , Callable , Generic , Type , TypeVar
18
+ from typing import Any , Awaitable , Callable , Generic , Type , TypeVar , Union
19
19
20
20
from playwright ._impl ._impl_to_api_mapping import ImplToApiMapping , ImplWrapper
21
21
22
22
mapping = ImplToApiMapping ()
23
23
24
24
25
25
T = TypeVar ("T" )
26
- Self = TypeVar ("Self" , bound = "AsyncBase " )
26
+ Self = TypeVar ("Self" , bound = "AsyncContextManager " )
27
27
28
28
29
29
class AsyncEventInfo (Generic [T ]):
30
- def __init__ (self , future : asyncio .Future ) -> None :
30
+ def __init__ (self , future : " asyncio.Future[T]" ) -> None :
31
31
self ._future = future
32
32
33
33
@property
@@ -39,13 +39,18 @@ def is_done(self) -> bool:
39
39
40
40
41
41
class AsyncEventContextManager (Generic [T ]):
42
- def __init__ (self , future : asyncio .Future ) -> None :
43
- self ._event : AsyncEventInfo = AsyncEventInfo (future )
42
+ def __init__ (self , future : " asyncio.Future[T]" ) -> None :
43
+ self ._event = AsyncEventInfo [ T ] (future )
44
44
45
45
async def __aenter__ (self ) -> AsyncEventInfo [T ]:
46
46
return self ._event
47
47
48
- async def __aexit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
48
+ async def __aexit__ (
49
+ self ,
50
+ exc_type : Type [BaseException ],
51
+ exc_val : BaseException ,
52
+ exc_tb : TracebackType ,
53
+ ) -> None :
49
54
await self ._event .value
50
55
51
56
@@ -68,17 +73,19 @@ def _wrap_handler(self, handler: Any) -> Callable[..., None]:
68
73
return mapping .wrap_handler (handler )
69
74
return handler
70
75
71
- def on (self , event : str , f : Any ) -> None :
76
+ def on (self , event : str , f : Callable [..., Union [ Awaitable [ None ], None ]] ) -> None :
72
77
"""Registers the function ``f`` to the event name ``event``."""
73
78
self ._impl_obj .on (event , self ._wrap_handler (f ))
74
79
75
- def once (self , event : str , f : Any ) -> None :
80
+ def once (self , event : str , f : Callable [..., Union [ Awaitable [ None ], None ]] ) -> None :
76
81
"""The same as ``self.on``, except that the listener is automatically
77
82
removed after being called.
78
83
"""
79
84
self ._impl_obj .once (event , self ._wrap_handler (f ))
80
85
81
- def remove_listener (self , event : str , f : Any ) -> None :
86
+ def remove_listener (
87
+ self , event : str , f : Callable [..., Union [Awaitable [None ], None ]]
88
+ ) -> None :
82
89
"""Removes the function ``f`` from ``event``."""
83
90
self ._impl_obj .remove_listener (event , self ._wrap_handler (f ))
84
91
@@ -93,4 +100,7 @@ async def __aexit__(
93
100
exc_val : BaseException ,
94
101
traceback : TracebackType ,
95
102
) -> None :
96
- await self .close () # type: ignore
103
+ await self .close ()
104
+
105
+ async def close (self : Self ) -> None :
106
+ ...
0 commit comments