Skip to content

Commit

Permalink
linting and better type hints for requestium session
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Mar 1, 2024
1 parent b0cbd94 commit d1f9c57
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions requestium/requestium_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tldextract
from selenium import webdriver
from selenium.webdriver import ChromeService
from selenium.webdriver.remote.webdriver import WebDriver

from .requestium_chrome import RequestiumChrome
from .requestium_response import RequestiumResponse
Expand All @@ -25,7 +26,8 @@ class Session(requests.Session):
"""

def __init__(
self, webdriver_path: str = None, headless: bool = False, default_timeout: float = 5, webdriver_options: dict = None, driver: webdriver = None
self, webdriver_path: str | None = None, headless: bool = False, default_timeout: float | int = 5, webdriver_options: dict | None = None,
driver: WebDriver | None = None
) -> None:
if webdriver_options is None:
webdriver_options = {}
Expand Down Expand Up @@ -68,21 +70,24 @@ def _start_chrome_browser(self, headless: bool = False) -> RequestiumChrome:

args = self.webdriver_options.get("arguments")
if isinstance(args, list):
[chrome_options.add_argument(arg) for arg in args]
for arg in args:
chrome_options.add_argument(arg)
elif args:
raise Exception(f"A list is needed to use 'arguments' option. Found {type(args)}")

extensions = self.webdriver_options.get("extensions")
if isinstance(extensions, list):
[chrome_options.add_extension(arg) for arg in extensions]
for arg in extensions:
chrome_options.add_extension(arg)

if "prefs" in self.webdriver_options:
prefs = self.webdriver_options["prefs"]
chrome_options.add_experimental_option("prefs", prefs)

experimental_options = self.webdriver_options.get("experimental_options")
if isinstance(experimental_options, dict):
[chrome_options.add_experimental_option(name, value) for name, value in experimental_options.items()]
for name, value in experimental_options.items():
chrome_options.add_experimental_option(name, value)

# Selenium updated webdriver.Chrome's arg and kwargs, to accept options, service, keep_alive
# since ChromeService is the only object where webdriver_path is mapped to executable_path, it must be
Expand All @@ -95,7 +100,7 @@ def _start_chrome_browser(self, headless: bool = False) -> RequestiumChrome:
default_timeout=self.default_timeout,
)

def transfer_session_cookies_to_driver(self, domain: str = None) -> None:
def transfer_session_cookies_to_driver(self, domain: str | None = None) -> None:
"""
Copies the Session's cookies into the webdriver
Expand Down

0 comments on commit d1f9c57

Please sign in to comment.