-
Notifications
You must be signed in to change notification settings - Fork 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
Conversation
@@ -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 comment
The 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 comment
The 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 cast
, it should only touch cast
.
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.
Okay
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.
Separated it to #790
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Accepts a Callable
@@ -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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Updated comment
@@ -37,7 +37,7 @@ async def path_after_finished(self) -> Optional[pathlib.Path]: | |||
return pathlib.Path(await self._channel.send("pathAfterFinished")) | |||
|
|||
async def save_as(self, path: Union[str, Path]) -> None: | |||
stream = cast(Stream, from_channel(await self._channel.send("saveAsStream"))) | |||
stream: Stream = from_channel(await self._channel.send("saveAsStream")) |
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...
stream: Stream = self
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
|
||
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Let's only deal with cast here.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Let's only deal with cast here.
"""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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -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 comment
The 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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
Please open it when it is ready for review, otherwise it is unclear whether you updated the patch or just replied to comments. |
I has separated the async base changes to another PR and waiting here for it to get merged before rebasing. Here no change is required just a rebasing |
Fixes #788