Skip to content

Commit a95c871

Browse files
authored
chore: start making types hermetic (microsoft#365)
1 parent 8ce0314 commit a95c871

26 files changed

+261
-250
lines changed

playwright/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
and for the async API [here](async_api.html).
2020
"""
2121

22-
import playwright.helper as helper
22+
import playwright.types as types
2323
from playwright.main import AsyncPlaywrightContextManager, SyncPlaywrightContextManager
2424

25-
Error = helper.Error
26-
TimeoutError = helper.TimeoutError
25+
Error = types.Error
26+
TimeoutError = types.TimeoutError
2727

2828

2929
def async_playwright() -> AsyncPlaywrightContextManager:

playwright/async_api.py

+26-29
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@
3737
from playwright.element_handle import ElementHandle as ElementHandleImpl
3838
from playwright.file_chooser import FileChooser as FileChooserImpl
3939
from playwright.frame import Frame as FrameImpl
40-
from playwright.helper import (
40+
from playwright.input import Keyboard as KeyboardImpl
41+
from playwright.input import Mouse as MouseImpl
42+
from playwright.input import Touchscreen as TouchscreenImpl
43+
from playwright.js_handle import JSHandle as JSHandleImpl
44+
from playwright.network import Request as RequestImpl
45+
from playwright.network import Response as ResponseImpl
46+
from playwright.network import Route as RouteImpl
47+
from playwright.network import WebSocket as WebSocketImpl
48+
from playwright.page import BindingCall as BindingCallImpl
49+
from playwright.page import Page as PageImpl
50+
from playwright.page import Worker as WorkerImpl
51+
from playwright.playwright import Playwright as PlaywrightImpl
52+
from playwright.selectors import Selectors as SelectorsImpl
53+
from playwright.types import (
4154
ConsoleMessageLocation,
55+
Cookie,
4256
Credentials,
4357
DeviceDescriptor,
4458
Error,
@@ -54,23 +68,8 @@
5468
RequestFailure,
5569
ResourceTiming,
5670
SelectOption,
57-
SetStorageState,
5871
StorageState,
59-
Viewport,
6072
)
61-
from playwright.input import Keyboard as KeyboardImpl
62-
from playwright.input import Mouse as MouseImpl
63-
from playwright.input import Touchscreen as TouchscreenImpl
64-
from playwright.js_handle import JSHandle as JSHandleImpl
65-
from playwright.network import Request as RequestImpl
66-
from playwright.network import Response as ResponseImpl
67-
from playwright.network import Route as RouteImpl
68-
from playwright.network import WebSocket as WebSocketImpl
69-
from playwright.page import BindingCall as BindingCallImpl
70-
from playwright.page import Page as PageImpl
71-
from playwright.page import Worker as WorkerImpl
72-
from playwright.playwright import Playwright as PlaywrightImpl
73-
from playwright.selectors import Selectors as SelectorsImpl
7473
from playwright.video import Video as VideoImpl
7574

7675
NoneType = type(None)
@@ -4548,7 +4547,7 @@ async def setViewportSize(self, width: int, height: int) -> NoneType:
45484547
await self._impl_obj.setViewportSize(width=width, height=height)
45494548
)
45504549

4551-
def viewportSize(self) -> typing.Union[Viewport, NoneType]:
4550+
def viewportSize(self) -> typing.Union[IntSize, NoneType]:
45524551
"""Page.viewportSize
45534552
45544553
Returns
@@ -5920,7 +5919,7 @@ async def newPage(self) -> "Page":
59205919

59215920
async def cookies(
59225921
self, urls: typing.Union[str, typing.List[str]] = None
5923-
) -> typing.List[typing.Dict]:
5922+
) -> typing.List[Cookie]:
59245923
"""BrowserContext.cookies
59255924
59265925
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
@@ -5933,23 +5932,21 @@ async def cookies(
59335932
59345933
Returns
59355934
-------
5936-
List[Dict]
5935+
List[{"name": str, "value": str, "url": Optional[str], "domain": Optional[str], "path": Optional[str], "expires": Optional[int], "httpOnly": Optional[bool], "secure": Optional[bool], "sameSite": Optional[Literal['Strict', 'Lax', 'None']]}]
59375936
"""
59385937
return mapping.from_maybe_impl(await self._impl_obj.cookies(urls=urls))
59395938

5940-
async def addCookies(self, cookies: typing.List[typing.Dict]) -> NoneType:
5939+
async def addCookies(self, cookies: typing.List[Cookie]) -> NoneType:
59415940
"""BrowserContext.addCookies
59425941
59435942
Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be
59445943
obtained via browserContext.cookies([urls]).
59455944
59465945
Parameters
59475946
----------
5948-
cookies : List[Dict]
5947+
cookies : List[{"name": str, "value": str, "url": Optional[str], "domain": Optional[str], "path": Optional[str], "expires": Optional[int], "httpOnly": Optional[bool], "secure": Optional[bool], "sameSite": Optional[Literal['Strict', 'Lax', 'None']]}]
59495948
"""
5950-
return mapping.from_maybe_impl(
5951-
await self._impl_obj.addCookies(cookies=mapping.to_impl(cookies))
5952-
)
5949+
return mapping.from_maybe_impl(await self._impl_obj.addCookies(cookies=cookies))
59535950

59545951
async def clearCookies(self) -> NoneType:
59555952
"""BrowserContext.clearCookies
@@ -6218,7 +6215,7 @@ async def storageState(self) -> StorageState:
62186215
62196216
Returns
62206217
-------
6221-
{"cookies": List[Dict], "origins": List[Dict]}
6218+
{"cookies": Optional[List[{"name": str, "value": str, "url": Optional[str], "domain": Optional[str], "path": Optional[str], "expires": Optional[int], "httpOnly": Optional[bool], "secure": Optional[bool], "sameSite": Optional[Literal['Strict', 'Lax', 'None']]}]], "origins": Optional[List[Dict]]}
62226219
"""
62236220
return mapping.from_maybe_impl(await self._impl_obj.storageState())
62246221

@@ -6430,7 +6427,7 @@ async def newContext(
64306427
videoSize: IntSize = None,
64316428
recordHar: RecordHarOptions = None,
64326429
recordVideo: RecordVideoOptions = None,
6433-
storageState: SetStorageState = None,
6430+
storageState: StorageState = None,
64346431
) -> "BrowserContext":
64356432
"""Browser.newContext
64366433
@@ -6481,7 +6478,7 @@ async def newContext(
64816478
Enables HAR recording for all pages into `recordHar.path` file. If not specified, the HAR is not recorded. Make sure to await browserContext.close() for the HAR to be saved.
64826479
recordVideo : Optional[{"dir": str, "size": Optional[{"width": int, "height": int}]}]
64836480
Enables video recording for all pages into `recordVideo.dir` directory. If not specified videos are not recorded. Make sure to await browserContext.close() for videos to be saved.
6484-
storageState : Optional[{"cookies": Optional[List[Dict]], "origins": Optional[List[Dict]]}]
6481+
storageState : Optional[{"cookies": Optional[List[{"name": str, "value": str, "url": Optional[str], "domain": Optional[str], "path": Optional[str], "expires": Optional[int], "httpOnly": Optional[bool], "secure": Optional[bool], "sameSite": Optional[Literal['Strict', 'Lax', 'None']]}]], "origins": Optional[List[Dict]]}]
64856482
Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState().
64866483
64876484
Returns
@@ -6542,7 +6539,7 @@ async def newPage(
65426539
videoSize: IntSize = None,
65436540
recordHar: RecordHarOptions = None,
65446541
recordVideo: RecordVideoOptions = None,
6545-
storageState: SetStorageState = None,
6542+
storageState: StorageState = None,
65466543
) -> "Page":
65476544
"""Browser.newPage
65486545
@@ -6596,7 +6593,7 @@ async def newPage(
65966593
Enables HAR recording for all pages into `recordHar.path` file. If not specified, the HAR is not recorded. Make sure to await browserContext.close() for the HAR to be saved.
65976594
recordVideo : Optional[{"dir": str, "size": Optional[{"width": int, "height": int}]}]
65986595
Enables video recording for all pages into `recordVideo.dir` directory. If not specified videos are not recorded. Make sure to await browserContext.close() for videos to be saved.
6599-
storageState : Optional[{"cookies": Optional[List[Dict]], "origins": Optional[List[Dict]]}]
6596+
storageState : Optional[{"cookies": Optional[List[{"name": str, "value": str, "url": Optional[str], "domain": Optional[str], "path": Optional[str], "expires": Optional[int], "httpOnly": Optional[bool], "secure": Optional[bool], "sameSite": Optional[Literal['Strict', 'Lax', 'None']]}]], "origins": Optional[List[Dict]]}]
66006597
Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState().
66016598
66026599
Returns

playwright/browser.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818

1919
from playwright.browser_context import BrowserContext
2020
from playwright.connection import ChannelOwner, from_channel
21-
from playwright.helper import (
22-
ColorScheme,
21+
from playwright.helper import ColorScheme, is_safe_close_error, locals_to_params
22+
from playwright.network import serialize_headers
23+
from playwright.page import Page
24+
from playwright.types import (
2325
Credentials,
2426
Geolocation,
2527
IntSize,
2628
ProxyServer,
2729
RecordHarOptions,
2830
RecordVideoOptions,
29-
SetStorageState,
30-
is_safe_close_error,
31-
locals_to_params,
31+
StorageState,
3232
)
33-
from playwright.network import serialize_headers
34-
from playwright.page import Page
3533

3634
if sys.version_info >= (3, 8): # pragma: no cover
3735
from typing import Literal
@@ -96,7 +94,7 @@ async def newContext(
9694
videoSize: IntSize = None,
9795
recordHar: RecordHarOptions = None,
9896
recordVideo: RecordVideoOptions = None,
99-
storageState: SetStorageState = None,
97+
storageState: StorageState = None,
10098
) -> BrowserContext:
10199
params = locals_to_params(locals())
102100
# Python is strict in which variables gets passed to methods. We get this
@@ -145,7 +143,7 @@ async def newPage(
145143
videoSize: IntSize = None,
146144
recordHar: RecordHarOptions = None,
147145
recordVideo: RecordVideoOptions = None,
148-
storageState: SetStorageState = None,
146+
storageState: StorageState = None,
149147
) -> Page:
150148
params = locals_to_params(locals())
151149
# Python is strict in which variables gets passed to methods. We get this

playwright/browser_context.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020
from playwright.connection import ChannelOwner, from_channel
2121
from playwright.event_context_manager import EventContextManagerImpl
2222
from playwright.helper import (
23-
Cookie,
24-
Error,
25-
Geolocation,
2623
PendingWaitEvent,
2724
RouteHandler,
2825
RouteHandlerEntry,
29-
StorageState,
3026
TimeoutSettings,
3127
URLMatch,
3228
URLMatcher,
@@ -35,6 +31,7 @@
3531
)
3632
from playwright.network import Request, Route, serialize_headers
3733
from playwright.page import BindingCall, Page
34+
from playwright.types import Cookie, Error, Geolocation, StorageState
3835
from playwright.wait_helper import WaitHelper
3936

4037
if TYPE_CHECKING: # pragma: no cover

playwright/browser_type.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@
1919
from playwright.browser import Browser
2020
from playwright.browser_context import BrowserContext
2121
from playwright.connection import ChannelOwner, from_channel
22-
from playwright.helper import (
23-
ColorScheme,
22+
from playwright.helper import ColorScheme, Env, locals_to_params, not_installed_error
23+
from playwright.network import serialize_headers
24+
from playwright.types import (
2425
Credentials,
25-
Env,
2626
Geolocation,
2727
IntSize,
2828
ProxyServer,
2929
RecordHarOptions,
3030
RecordVideoOptions,
31-
locals_to_params,
32-
not_installed_error,
3331
)
34-
from playwright.network import serialize_headers
3532

3633
if sys.version_info >= (3, 8): # pragma: no cover
3734
from typing import Literal

playwright/console_message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from typing import Dict, List
1616

1717
from playwright.connection import ChannelOwner, from_channel
18-
from playwright.helper import ConsoleMessageLocation
1918
from playwright.js_handle import JSHandle
19+
from playwright.types import ConsoleMessageLocation
2020

2121

2222
class ConsoleMessage(ChannelOwner):

playwright/element_handle.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121

2222
from playwright.connection import ChannelOwner, from_nullable_channel
2323
from playwright.helper import (
24-
FilePayload,
25-
FloatRect,
2624
KeyboardModifier,
2725
MouseButton,
28-
MousePosition,
29-
SelectOption,
3026
SetFilePayload,
3127
locals_to_params,
3228
)
@@ -36,6 +32,7 @@
3632
parse_result,
3733
serialize_argument,
3834
)
35+
from playwright.types import FilePayload, FloatRect, MousePosition, SelectOption
3936

4037
if sys.version_info >= (3, 8): # pragma: no cover
4138
from typing import Literal

playwright/file_chooser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from typing import TYPE_CHECKING, List, Union
1616

17-
from playwright.helper import FilePayload
17+
from playwright.types import FilePayload
1818

1919
if TYPE_CHECKING: # pragma: no cover
2020
from playwright.element_handle import ElementHandle

playwright/frame.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929
from playwright.event_context_manager import EventContextManagerImpl
3030
from playwright.helper import (
3131
DocumentLoadState,
32-
Error,
33-
FilePayload,
3432
FrameNavigatedEvent,
3533
KeyboardModifier,
3634
MouseButton,
37-
MousePosition,
3835
URLMatch,
3936
URLMatcher,
4037
is_function_body,
@@ -48,6 +45,7 @@
4845
serialize_argument,
4946
)
5047
from playwright.network import Response
48+
from playwright.types import Error, FilePayload, MousePosition
5149
from playwright.wait_helper import WaitHelper
5250

5351
if sys.version_info >= (3, 8): # pragma: no cover

0 commit comments

Comments
 (0)