Skip to content

fix: improve typing for SyncBase and AsyncBase #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions playwright/_impl/_async_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
import asyncio
import traceback
from types import TracebackType
from typing import Any, Awaitable, Callable, Generic, Type, TypeVar
from typing import Any, Awaitable, Callable, Generic, Type, TypeVar, Union

from playwright._impl._impl_to_api_mapping import ImplToApiMapping, ImplWrapper

mapping = ImplToApiMapping()


T = TypeVar("T")
Self = TypeVar("Self", bound="AsyncBase")
Self = TypeVar("Self", bound="AsyncContextManager")


class AsyncEventInfo(Generic[T]):
def __init__(self, future: asyncio.Future) -> None:
def __init__(self, future: "asyncio.Future[T]") -> None:
self._future = future

@property
Expand All @@ -39,13 +39,18 @@ def is_done(self) -> bool:


class AsyncEventContextManager(Generic[T]):
def __init__(self, future: asyncio.Future) -> None:
self._event: AsyncEventInfo = AsyncEventInfo(future)
def __init__(self, future: "asyncio.Future[T]") -> None:
self._event = AsyncEventInfo[T](future)

async def __aenter__(self) -> AsyncEventInfo[T]:
return self._event

async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
async def __aexit__(
self,
exc_type: Type[BaseException],
exc_val: BaseException,
exc_tb: TracebackType,
) -> None:
await self._event.value


Expand All @@ -68,17 +73,19 @@ def _wrap_handler(self, handler: Any) -> Callable[..., None]:
return mapping.wrap_handler(handler)
return handler

def on(self, event: str, f: Any) -> None:
def on(self, event: str, f: Callable[..., Union[Awaitable[None], None]]) -> None:
"""Registers the function ``f`` to the event name ``event``."""
self._impl_obj.on(event, self._wrap_handler(f))

def once(self, event: str, f: Any) -> None:
def once(self, event: str, f: Callable[..., Union[Awaitable[None], None]]) -> None:
"""The same as ``self.on``, except that the listener is automatically
removed after being called.
"""
self._impl_obj.once(event, self._wrap_handler(f))

def remove_listener(self, event: str, f: Any) -> None:
def remove_listener(
self, event: str, f: Callable[..., Union[Awaitable[None], None]]
) -> None:
"""Removes the function ``f`` from ``event``."""
self._impl_obj.remove_listener(event, self._wrap_handler(f))

Expand All @@ -93,4 +100,7 @@ async def __aexit__(
exc_val: BaseException,
traceback: TracebackType,
) -> None:
await self.close() # type: ignore
await self.close()

async def close(self: Self) -> None:
...
30 changes: 19 additions & 11 deletions playwright/_impl/_sync_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@


T = TypeVar("T")
Self = TypeVar("Self")
Self = TypeVar("Self", bound="SyncContextManager")


class EventInfo(Generic[T]):
def __init__(self, sync_base: "SyncBase", future: asyncio.Future) -> None:
def __init__(self, sync_base: "SyncBase", future: "asyncio.Future[T]") -> None:
self._sync_base = sync_base
self._value: Optional[T] = None
self._exception = None
self._exception: Optional[Exception] = None
self._future = future
g_self = greenlet.getcurrent()

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


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

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

def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
def __exit__(
self,
exc_type: Type[BaseException],
exc_val: BaseException,
exc_tb: TracebackType,
) -> None:
self._event.value


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

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

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

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

Expand Down Expand Up @@ -167,4 +172,7 @@ def __exit__(
exc_val: BaseException,
traceback: TracebackType,
) -> None:
self.close() # type: ignore
self.close()

def close(self: Self) -> None:
...