-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore(types): improve typing #789
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only deal with cast here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay |
||
|
||
|
||
class AsyncEventInfo(Generic[T]): | ||
def __init__(self, future: asyncio.Future) -> None: | ||
def __init__(self, future: "asyncio.Future[T]") -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only deal with cast here. |
||
self._future = future | ||
|
||
@property | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only deal with cast here. |
||
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 | ||
|
||
|
||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will accept both sync and async event listeners There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only do one thing at a time in each PR. If this PR is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separated it to #790 |
||
"""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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
"""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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
) -> None: | ||
"""Removes the function ``f`` from ``event``.""" | ||
self._impl_obj.remove_listener(event, self._wrap_handler(f)) | ||
|
||
|
@@ -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: | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,11 +319,12 @@ def _replace_guids_with_channels(self, payload: Any) -> Any: | |
return payload | ||
|
||
|
||
def from_channel(channel: Channel) -> Any: | ||
def from_channel(channel: Channel) -> ChannelOwner: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works currently but ChannelOwner is better than Any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this change to move to cast. It does not hurt, but this comment is misleading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated comment |
||
assert channel._object | ||
return channel._object | ||
|
||
|
||
def from_nullable_channel(channel: Optional[Channel]) -> Optional[Any]: | ||
def from_nullable_channel(channel: Optional[Channel]) -> Optional[ChannelOwner]: | ||
return channel._object if channel else None | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accepts a Callable |
||
"""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)) | ||
|
||
|
@@ -167,4 +172,7 @@ def __exit__( | |
exc_val: BaseException, | ||
traceback: TracebackType, | ||
) -> None: | ||
self.close() # type: ignore | ||
self.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing at a time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay |
||
|
||
def close(self: Self) -> None: | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried following locally and mypy was happy...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stream inherits from ChannelOwner and from_channel returns ChannelOwner hence it is happy