1515import asyncio
1616import traceback
1717from types import TracebackType
18- from typing import Any , Awaitable , Callable , Generic , Type , TypeVar
18+ from typing import Any , Awaitable , Callable , Generic , Type , TypeVar , Union
1919
2020from playwright ._impl ._impl_to_api_mapping import ImplToApiMapping , ImplWrapper
2121
2222mapping = ImplToApiMapping ()
2323
2424
2525T = TypeVar ("T" )
26- Self = TypeVar ("Self" , bound = "AsyncBase " )
26+ Self = TypeVar ("Self" , bound = "AsyncContextManager " )
2727
2828
2929class AsyncEventInfo (Generic [T ]):
30- def __init__ (self , future : asyncio .Future ) -> None :
30+ def __init__ (self , future : " asyncio.Future[T]" ) -> None :
3131 self ._future = future
3232
3333 @property
@@ -39,13 +39,18 @@ def is_done(self) -> bool:
3939
4040
4141class 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 )
4444
4545 async def __aenter__ (self ) -> AsyncEventInfo [T ]:
4646 return self ._event
4747
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 :
4954 await self ._event .value
5055
5156
@@ -68,17 +73,19 @@ def _wrap_handler(self, handler: Any) -> Callable[..., None]:
6873 return mapping .wrap_handler (handler )
6974 return handler
7075
71- def on (self , event : str , f : Any ) -> None :
76+ def on (self , event : str , f : Callable [..., Union [ Awaitable [ None ], None ]] ) -> None :
7277 """Registers the function ``f`` to the event name ``event``."""
7378 self ._impl_obj .on (event , self ._wrap_handler (f ))
7479
75- def once (self , event : str , f : Any ) -> None :
80+ def once (self , event : str , f : Callable [..., Union [ Awaitable [ None ], None ]] ) -> None :
7681 """The same as ``self.on``, except that the listener is automatically
7782 removed after being called.
7883 """
7984 self ._impl_obj .once (event , self ._wrap_handler (f ))
8085
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 :
8289 """Removes the function ``f`` from ``event``."""
8390 self ._impl_obj .remove_listener (event , self ._wrap_handler (f ))
8491
@@ -93,4 +100,7 @@ async def __aexit__(
93100 exc_val : BaseException ,
94101 traceback : TracebackType ,
95102 ) -> None :
96- await self .close () # type: ignore
103+ await self .close ()
104+
105+ async def close (self : Self ) -> None :
106+ ...
0 commit comments