Skip to content

Commit 673eed8

Browse files
fix: improve typing for SyncBase and AsyncBase (microsoft#790)
1 parent 809727d commit 673eed8

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

playwright/_impl/_async_base.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
import asyncio
1616
import traceback
1717
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
1919

2020
from playwright._impl._impl_to_api_mapping import ImplToApiMapping, ImplWrapper
2121

2222
mapping = ImplToApiMapping()
2323

2424

2525
T = TypeVar("T")
26-
Self = TypeVar("Self", bound="AsyncBase")
26+
Self = TypeVar("Self", bound="AsyncContextManager")
2727

2828

2929
class 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

4141
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)
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+
...

playwright/_impl/_sync_base.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@
3636

3737

3838
T = TypeVar("T")
39-
Self = TypeVar("Self")
39+
Self = TypeVar("Self", bound="SyncContextManager")
4040

4141

4242
class EventInfo(Generic[T]):
43-
def __init__(self, sync_base: "SyncBase", future: asyncio.Future) -> None:
43+
def __init__(self, sync_base: "SyncBase", future: "asyncio.Future[T]") -> None:
4444
self._sync_base = sync_base
4545
self._value: Optional[T] = None
46-
self._exception = None
46+
self._exception: Optional[Exception] = None
4747
self._future = future
4848
g_self = greenlet.getcurrent()
4949

50-
def done_callback(task: Any) -> None:
50+
def done_callback(task: "asyncio.Future[T]") -> None:
5151
try:
5252
self._value = mapping.from_maybe_impl(self._future.result())
5353
except Exception as e:
@@ -71,13 +71,18 @@ def is_done(self) -> bool:
7171

7272

7373
class EventContextManager(Generic[T]):
74-
def __init__(self, sync_base: "SyncBase", future: asyncio.Future) -> None:
75-
self._event: EventInfo = EventInfo(sync_base, future)
74+
def __init__(self, sync_base: "SyncBase", future: "asyncio.Future[T]") -> None:
75+
self._event = EventInfo[T](sync_base, future)
7676

7777
def __enter__(self) -> EventInfo[T]:
7878
return self._event
7979

80-
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
80+
def __exit__(
81+
self,
82+
exc_type: Type[BaseException],
83+
exc_val: BaseException,
84+
exc_tb: TracebackType,
85+
) -> None:
8186
self._event.value
8287

8388

@@ -110,17 +115,17 @@ def _wrap_handler(self, handler: Any) -> Callable[..., None]:
110115
return mapping.wrap_handler(handler)
111116
return handler
112117

113-
def on(self, event: str, f: Any) -> None:
118+
def on(self, event: str, f: Callable[..., None]) -> None:
114119
"""Registers the function ``f`` to the event name ``event``."""
115120
self._impl_obj.on(event, self._wrap_handler(f))
116121

117-
def once(self, event: str, f: Any) -> None:
122+
def once(self, event: str, f: Callable[..., None]) -> None:
118123
"""The same as ``self.on``, except that the listener is automatically
119124
removed after being called.
120125
"""
121126
self._impl_obj.once(event, self._wrap_handler(f))
122127

123-
def remove_listener(self, event: str, f: Any) -> None:
128+
def remove_listener(self, event: str, f: Callable[..., None]) -> None:
124129
"""Removes the function ``f`` from ``event``."""
125130
self._impl_obj.remove_listener(event, self._wrap_handler(f))
126131

@@ -167,4 +172,7 @@ def __exit__(
167172
exc_val: BaseException,
168173
traceback: TracebackType,
169174
) -> None:
170-
self.close() # type: ignore
175+
self.close()
176+
177+
def close(self: Self) -> None:
178+
...

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy