Skip to content

Commit 8f9d4c3

Browse files
committed
Add browser_type assertions
1 parent b3e5199 commit 8f9d4c3

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

playwright/_impl/_browser.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ def _did_create_context(self, context: BrowserContext) -> None:
106106

107107
def _setup_browser_context(self, context: BrowserContext) -> None:
108108
context._tracing._traces_dir = self._traces_dir
109-
if self._browser_type:
110-
self._browser_type._playwright.selectors._contexts_for_selectors.add(
111-
context
112-
)
109+
assert self._browser_type is not None
110+
self._browser_type._playwright.selectors._contexts_for_selectors.add(context)
113111

114112
def _on_close(self) -> None:
115113
self._is_connected = False
@@ -121,10 +119,7 @@ def contexts(self) -> List[BrowserContext]:
121119

122120
@property
123121
def browser_type(self) -> "BrowserType":
124-
if not self._browser_type:
125-
raise RuntimeError(
126-
"_browser_type is not set. Make sure _connect_to_browser_type() is called on initialization."
127-
)
122+
assert self._browser_type is not None
128123
return self._browser_type
129124

130125
def is_connected(self) -> bool:
@@ -170,8 +165,8 @@ async def new_context(
170165
clientCertificates: List[ClientCertificate] = None,
171166
) -> BrowserContext:
172167
params = locals_to_params(locals())
173-
if self._browser_type:
174-
await self._browser_type._prepare_browser_context_params(params)
168+
assert self._browser_type is not None
169+
await self._browser_type._prepare_browser_context_params(params)
175170

176171
channel = await self._channel.send("newContext", params)
177172
context = cast(BrowserContext, from_channel(channel))

playwright/_impl/_browser_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ def _on_close(self) -> None:
570570
if self._browser:
571571
if self in self._browser._contexts:
572572
self._browser._contexts.remove(self)
573+
assert self._browser._browser_type is not None
573574
if (
574-
self._browser._browser_type
575-
and self
575+
self
576576
in self._browser._browser_type._playwright.selectors._contexts_for_selectors
577577
):
578578
self._browser._browser_type._playwright.selectors._contexts_for_selectors.remove(

0 commit comments

Comments
 (0)