From 982c703c55dfccd7486636d13fced7b97a55ec43 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 8 Nov 2022 14:22:06 -0800 Subject: [PATCH] chore: various type fixes (#1636) --- playwright/_impl/_browser_context.py | 2 +- playwright/_impl/_browser_type.py | 2 +- playwright/_impl/_connection.py | 2 +- playwright/_impl/_js_handle.py | 6 +++--- playwright/_impl/_json_pipe.py | 1 + playwright/_impl/_network.py | 4 +++- playwright/_impl/_page.py | 4 ++-- playwright/_impl/_playwright.py | 2 +- playwright/sync_api/_context_manager.py | 7 ++++--- 9 files changed, 17 insertions(+), 13 deletions(-) diff --git a/playwright/_impl/_browser_context.py b/playwright/_impl/_browser_context.py index 38f375f4d..2afbab914 100644 --- a/playwright/_impl/_browser_context.py +++ b/playwright/_impl/_browser_context.py @@ -320,7 +320,7 @@ async def _record_into_har( page: Optional[Page] = None, url: Union[Pattern[str], str] = None, ) -> None: - params = { + params: Dict[str, Any] = { "options": prepare_record_har_options( { "recordHarPath": har, diff --git a/playwright/_impl/_browser_type.py b/playwright/_impl/_browser_type.py index aeeab8422..afe69980d 100644 --- a/playwright/_impl/_browser_type.py +++ b/playwright/_impl/_browser_type.py @@ -54,7 +54,7 @@ def __init__( self, parent: ChannelOwner, type: str, guid: str, initializer: Dict ) -> None: super().__init__(parent, type, guid, initializer) - _playwright: "Playwright" + self._playwright: "Playwright" def __repr__(self) -> str: return f"" diff --git a/playwright/_impl/_connection.py b/playwright/_impl/_connection.py index 0243d85a1..dd4d2eac0 100644 --- a/playwright/_impl/_connection.py +++ b/playwright/_impl/_connection.py @@ -290,7 +290,7 @@ def dispatch(self, msg: ParsedMessagePayload) -> None: return guid = msg["guid"] - method = msg.get("method") + method = msg["method"] params = msg.get("params") if method == "__create__": assert params diff --git a/playwright/_impl/_js_handle.py b/playwright/_impl/_js_handle.py index d0b72dcc7..51e1ee18a 100644 --- a/playwright/_impl/_js_handle.py +++ b/playwright/_impl/_js_handle.py @@ -17,7 +17,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional from urllib.parse import ParseResult, urlparse, urlunparse -from playwright._impl._connection import ChannelOwner, from_channel +from playwright._impl._connection import Channel, ChannelOwner, from_channel from playwright._impl._map import Map if TYPE_CHECKING: # pragma: no cover @@ -107,7 +107,7 @@ async def json_value(self) -> Any: def serialize_value( - value: Any, handles: List[JSHandle], visitor_info: Optional[VisitorInfo] = None + value: Any, handles: List[Channel], visitor_info: Optional[VisitorInfo] = None ) -> Any: if visitor_info is None: visitor_info = VisitorInfo() @@ -159,7 +159,7 @@ def serialize_value( def serialize_argument(arg: Serializable = None) -> Any: - handles: List[JSHandle] = [] + handles: List[Channel] = [] value = serialize_value(arg, handles) return dict(value=value, handles=handles) diff --git a/playwright/_impl/_json_pipe.py b/playwright/_impl/_json_pipe.py index a237a63df..311c284e8 100644 --- a/playwright/_impl/_json_pipe.py +++ b/playwright/_impl/_json_pipe.py @@ -33,6 +33,7 @@ def __init__( Transport.__init__(self, loop) self._stop_requested = False self._pipe_channel = pipe_channel + self._loop: asyncio.AbstractEventLoop def request_stop(self) -> None: self._stop_requested = True diff --git a/playwright/_impl/_network.py b/playwright/_impl/_network.py index f89626b6e..46fadf62c 100644 --- a/playwright/_impl/_network.py +++ b/playwright/_impl/_network.py @@ -54,6 +54,7 @@ if TYPE_CHECKING: # pragma: no cover from playwright._impl._fetch import APIResponse from playwright._impl._frame import Frame + from playwright._impl._page import Page def serialize_headers(headers: Dict[str, str]) -> HeadersArray: @@ -513,6 +514,7 @@ def __init__( ) -> None: super().__init__(parent, type, guid, initializer) self._is_closed = False + self._page = cast("Page", parent) self._channel.on( "frameSent", lambda params: self._on_frame_sent(params["opcode"], params["data"]), @@ -555,7 +557,7 @@ def expect_event( wait_helper.reject_on_event( self, WebSocket.Events.Error, Error("Socket error") ) - wait_helper.reject_on_event(self._parent, "close", Error("Page closed")) + wait_helper.reject_on_event(self._page, "close", Error("Page closed")) wait_helper.wait_for_event(self, event, predicate) return EventContextManagerImpl(wait_helper.result()) diff --git a/playwright/_impl/_page.py b/playwright/_impl/_page.py index 52c31830f..2e4c27c7e 100644 --- a/playwright/_impl/_page.py +++ b/playwright/_impl/_page.py @@ -133,7 +133,7 @@ def __init__( self, parent: ChannelOwner, type: str, guid: str, initializer: Dict ) -> None: super().__init__(parent, type, guid, initializer) - self._browser_context: BrowserContext = parent + self._browser_context = cast("BrowserContext", parent) self.accessibility = Accessibility(self._channel) self.keyboard = Keyboard(self._channel) self.mouse = Mouse(self._channel) @@ -1268,7 +1268,7 @@ async def call(self, func: Callable) -> None: ) -def trim_url(param: URLMatchRequest) -> Optional[str]: +def trim_url(param: Union[URLMatchRequest, URLMatchResponse]) -> Optional[str]: if isinstance(param, re.Pattern): return trim_end(param.pattern) if isinstance(param, str): diff --git a/playwright/_impl/_playwright.py b/playwright/_impl/_playwright.py index f9fe7617f..746b2e830 100644 --- a/playwright/_impl/_playwright.py +++ b/playwright/_impl/_playwright.py @@ -64,7 +64,7 @@ def __getitem__(self, value: str) -> "BrowserType": return self.webkit raise ValueError("Invalid browser " + value) - def _set_selectors(self, selectors: SelectorsOwner) -> None: + def _set_selectors(self, selectors: Selectors) -> None: selectors_owner = from_channel(self._initializer["selectors"]) self.selectors._remove_channel(selectors_owner) self.selectors = selectors diff --git a/playwright/sync_api/_context_manager.py b/playwright/sync_api/_context_manager.py index 0fe9b552b..5cae1ea1d 100644 --- a/playwright/sync_api/_context_manager.py +++ b/playwright/sync_api/_context_manager.py @@ -14,12 +14,12 @@ import asyncio import sys -from typing import Any, Optional +from typing import Any, Optional, cast from greenlet import greenlet from playwright._impl._api_types import Error -from playwright._impl._connection import Connection +from playwright._impl._connection import ChannelOwner, Connection from playwright._impl._driver import compute_driver_executable from playwright._impl._object_factory import create_remote_object from playwright._impl._playwright import Playwright @@ -77,7 +77,8 @@ def greenlet_main() -> None: g_self = greenlet.getcurrent() - def callback_wrapper(playwright_impl: Playwright) -> None: + def callback_wrapper(channel_owner: ChannelOwner) -> None: + playwright_impl = cast(Playwright, channel_owner) self._playwright = SyncPlaywright(playwright_impl) g_self.switch()