Add pyupgrade pre-commit hook + modernize python code (#2611)
This commit is contained in:
@@ -162,7 +162,7 @@ class BrowserContextFactory:
|
||||
preference_template = f"{SKYVERN_DIR}/webeye/chromium_preferences.json"
|
||||
|
||||
preference_file_content = ""
|
||||
with open(preference_template, "r") as f:
|
||||
with open(preference_template) as f:
|
||||
preference_file_content = f.read()
|
||||
preference_file_content = preference_file_content.replace("MASK_SAVEFILE_DEFAULT_DIRECTORY", download_dir)
|
||||
preference_file_content = preference_file_content.replace("MASK_DOWNLOAD_DEFAULT_DIRECTORY", download_dir)
|
||||
@@ -281,7 +281,7 @@ class BrowserContextFactory:
|
||||
class VideoArtifact(BaseModel):
|
||||
video_path: str | None = None
|
||||
video_artifact_id: str | None = None
|
||||
video_data: bytes = bytes()
|
||||
video_data: bytes = b""
|
||||
|
||||
|
||||
class BrowserArtifacts(BaseModel):
|
||||
@@ -385,7 +385,7 @@ def _is_port_in_use(port: int) -> bool:
|
||||
try:
|
||||
s.bind(("localhost", port))
|
||||
return False
|
||||
except socket.error:
|
||||
except OSError:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, Optional, Tuple
|
||||
|
||||
import structlog
|
||||
from playwright._impl._errors import TargetClosedError
|
||||
@@ -22,7 +21,7 @@ class BrowserSession:
|
||||
|
||||
class PersistentSessionsManager:
|
||||
instance: PersistentSessionsManager | None = None
|
||||
_browser_sessions: Dict[str, BrowserSession] = dict()
|
||||
_browser_sessions: dict[str, BrowserSession] = dict()
|
||||
database: AgentDB
|
||||
|
||||
def __new__(cls, database: AgentDB) -> PersistentSessionsManager:
|
||||
@@ -82,7 +81,7 @@ class PersistentSessionsManager:
|
||||
organization_id=organization_id,
|
||||
)
|
||||
|
||||
async def get_network_info(self, session_id: str) -> Tuple[Optional[int], Optional[str]]:
|
||||
async def get_network_info(self, session_id: str) -> tuple[int | None, str | None]:
|
||||
"""Returns cdp port and ip address of the browser session"""
|
||||
browser_session = self._browser_sessions.get(session_id)
|
||||
if browser_session:
|
||||
|
||||
@@ -75,7 +75,7 @@ def load_js_script() -> str:
|
||||
try:
|
||||
# TODO: Implement TS of domUtils.js and use the complied JS file instead of the raw JS file.
|
||||
# This will allow our code to be type safe.
|
||||
with open(path, "r") as f:
|
||||
with open(path) as f:
|
||||
return f.read()
|
||||
except FileNotFoundError as e:
|
||||
LOG.exception("Failed to load the JS script", path=path)
|
||||
|
||||
@@ -34,9 +34,7 @@ TEXT_INPUT_DELAY = 10 # 10ms between each character input
|
||||
TEXT_PRESS_MAX_LENGTH = 20
|
||||
|
||||
|
||||
async def resolve_locator(
|
||||
scrape_page: ScrapedPage, page: Page, frame: str, css: str
|
||||
) -> typing.Tuple[Locator, Page | Frame]:
|
||||
async def resolve_locator(scrape_page: ScrapedPage, page: Page, frame: str, css: str) -> tuple[Locator, Page | Frame]:
|
||||
iframe_path: list[str] = []
|
||||
|
||||
while frame != "main.frame":
|
||||
@@ -335,10 +333,10 @@ class SkyvernElement:
|
||||
def get_frame_id(self) -> str:
|
||||
return self._frame_id
|
||||
|
||||
def get_attributes(self) -> typing.Dict:
|
||||
def get_attributes(self) -> dict:
|
||||
return self._attributes
|
||||
|
||||
def get_options(self) -> typing.List[SkyvernOptionType]:
|
||||
def get_options(self) -> list[SkyvernOptionType]:
|
||||
options = self.__static_element.get("options", None)
|
||||
if options is None:
|
||||
return []
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any
|
||||
|
||||
import structlog
|
||||
from playwright._impl._errors import TimeoutError
|
||||
@@ -21,7 +21,7 @@ def load_js_script() -> str:
|
||||
try:
|
||||
# TODO: Implement TS of domUtils.js and use the complied JS file instead of the raw JS file.
|
||||
# This will allow our code to be type safe.
|
||||
with open(path, "r") as f:
|
||||
with open(path) as f:
|
||||
return f.read()
|
||||
except FileNotFoundError as e:
|
||||
LOG.exception("Failed to load the JS script", path=path)
|
||||
@@ -43,7 +43,7 @@ async def _current_viewpoint_screenshot_helper(
|
||||
await page.wait_for_load_state(timeout=settings.BROWSER_LOADING_TIMEOUT_MS)
|
||||
LOG.debug("Page is fully loaded, agent is about to take screenshots")
|
||||
start_time = time.time()
|
||||
screenshot: bytes = bytes()
|
||||
screenshot: bytes = b""
|
||||
if file_path:
|
||||
screenshot = await page.screenshot(
|
||||
path=file_path,
|
||||
@@ -77,14 +77,14 @@ async def _scrolling_screenshots_helper(
|
||||
url: str | None = None,
|
||||
draw_boxes: bool = False,
|
||||
max_number: int = settings.MAX_NUM_SCREENSHOTS,
|
||||
) -> List[bytes]:
|
||||
) -> list[bytes]:
|
||||
skyvern_page = await SkyvernFrame.create_instance(frame=page)
|
||||
# page is the main frame and the index must be 0
|
||||
assert isinstance(skyvern_page.frame, Page)
|
||||
frame = "main.frame"
|
||||
frame_index = 0
|
||||
|
||||
screenshots: List[bytes] = []
|
||||
screenshots: list[bytes] = []
|
||||
if await skyvern_page.is_window_scrollable():
|
||||
scroll_y_px_old = -30.0
|
||||
scroll_y_px = await skyvern_page.scroll_to_top(draw_boxes=draw_boxes, frame=frame, frame_index=frame_index)
|
||||
@@ -161,7 +161,7 @@ class SkyvernFrame:
|
||||
draw_boxes: bool = False,
|
||||
max_number: int = settings.MAX_NUM_SCREENSHOTS,
|
||||
scroll: bool = True,
|
||||
) -> List[bytes]:
|
||||
) -> list[bytes]:
|
||||
if not scroll:
|
||||
return [await _current_viewpoint_screenshot_helper(page=page)]
|
||||
|
||||
@@ -199,7 +199,7 @@ class SkyvernFrame:
|
||||
js_script = "(element) => scrollToElementTop(element)"
|
||||
return await self.evaluate(frame=self.frame, expression=js_script, arg=element)
|
||||
|
||||
async def parse_element_from_html(self, frame: str, element: ElementHandle, interactable: bool) -> Dict:
|
||||
async def parse_element_from_html(self, frame: str, element: ElementHandle, interactable: bool) -> dict:
|
||||
js_script = "async ([frame, element, interactable]) => await buildElementObject(frame, element, interactable)"
|
||||
return await self.evaluate(frame=self.frame, expression=js_script, arg=[frame, element, interactable])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user