Skip to content

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

Closed
wants to merge 1 commit into from
Closed

chore(types): improve typing #789

wants to merge 1 commit into from

Conversation

kumaraditya303
Copy link
Contributor

Fixes #788

@@ -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:
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Copy link
Contributor Author

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:
Copy link
Contributor Author

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:
Copy link
Contributor Author

@kumaraditya303 kumaraditya303 Jul 3, 2021

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

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated comment

@kumaraditya303 kumaraditya303 marked this pull request as draft July 3, 2021 11:08
@kumaraditya303 kumaraditya303 marked this pull request as ready for review July 3, 2021 11:59
@@ -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"))
Copy link
Member

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

Copy link
Contributor Author

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")
Copy link
Member

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.

Copy link
Contributor Author

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:
Copy link
Member

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:
Copy link
Member

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:
Copy link
Member

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]]
Copy link
Member

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:
Copy link
Member

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()
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

@pavelfeldman
Copy link
Member

Please open it when it is ready for review, otherwise it is unclear whether you updated the patch or just replied to comments.

@kumaraditya303
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API] Improve typings and reduce runtime casts
2 participants