From 05bce9b4c088d939d4a25a33e0d014d3f3a67473 Mon Sep 17 00:00:00 2001 From: Navin Chandra <98466550+navin772@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:03:03 +0530 Subject: [PATCH] fix type errors for `pointer_input.py`, `wheel_input.py` and `firefox/options.py` (#14476) * fix type errors fro `firefox/options.py` * fix more mypy errors --------- Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com> --- py/selenium/webdriver/chromium/service.py | 5 ++++- .../webdriver/common/actions/pointer_input.py | 3 ++- .../webdriver/common/actions/wheel_input.py | 2 +- py/selenium/webdriver/firefox/options.py | 15 ++++++++++----- py/selenium/webdriver/wpewebkit/service.py | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/py/selenium/webdriver/chromium/service.py b/py/selenium/webdriver/chromium/service.py index b37850cf100a2..fc7d165f2b8f0 100644 --- a/py/selenium/webdriver/chromium/service.py +++ b/py/selenium/webdriver/chromium/service.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. import typing +from io import IOBase from selenium.types import SubprocessStdAlias from selenium.webdriver.common import service @@ -44,7 +45,9 @@ def __init__( if isinstance(log_output, str): self.service_args.append(f"--log-path={log_output}") - self.log_output = None + self.log_output: typing.Optional[IOBase] = None + elif isinstance(log_output, IOBase): + self.log_output = log_output else: self.log_output = log_output diff --git a/py/selenium/webdriver/common/actions/pointer_input.py b/py/selenium/webdriver/common/actions/pointer_input.py index 89cb6ac3a5185..ce5e1c53c91f9 100644 --- a/py/selenium/webdriver/common/actions/pointer_input.py +++ b/py/selenium/webdriver/common/actions/pointer_input.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. import typing +from typing import Union from selenium.common.exceptions import InvalidArgumentException from selenium.webdriver.remote.webelement import WebElement @@ -60,7 +61,7 @@ def create_pointer_up(self, button): def create_pointer_cancel(self): self.add_action({"type": "pointerCancel"}) - def create_pause(self, pause_duration: float) -> None: + def create_pause(self, pause_duration: Union[int, float] = 0) -> None: self.add_action({"type": "pause", "duration": int(pause_duration * 1000)}) def encode(self): diff --git a/py/selenium/webdriver/common/actions/wheel_input.py b/py/selenium/webdriver/common/actions/wheel_input.py index 6f08f6754ad54..a072e825be4b9 100644 --- a/py/selenium/webdriver/common/actions/wheel_input.py +++ b/py/selenium/webdriver/common/actions/wheel_input.py @@ -73,5 +73,5 @@ def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: in } ) - def create_pause(self, pause_duration: float) -> None: + def create_pause(self, pause_duration: Union[int, float] = 0) -> None: self.add_action({"type": "pause", "duration": int(pause_duration * 1000)}) diff --git a/py/selenium/webdriver/firefox/options.py b/py/selenium/webdriver/firefox/options.py index b16622b03dd93..4996af551b361 100644 --- a/py/selenium/webdriver/firefox/options.py +++ b/py/selenium/webdriver/firefox/options.py @@ -14,6 +14,9 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from typing import Any +from typing import Dict +from typing import Optional from typing import Union from typing_extensions import deprecated @@ -44,7 +47,7 @@ def __init__(self) -> None: # Firefox 129 onwards the CDP protocol will not be enabled by default. Setting this preference will enable it. # https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/. self._preferences["remote.active-protocols"] = 3 - self._profile = None + self._profile: Optional[FirefoxProfile] = None self.log = Log() @property @@ -60,7 +63,7 @@ def binary(self, new_binary: Union[str, FirefoxBinary]) -> None: ``FirefoxBinary`` instance.""" if isinstance(new_binary, FirefoxBinary): new_binary = new_binary._start_cmd - self.binary_location = new_binary + self.binary_location = str(new_binary) @property def binary_location(self) -> str: @@ -84,7 +87,7 @@ def set_preference(self, name: str, value: Union[str, int, bool]): self._preferences[name] = value @property - def profile(self) -> FirefoxProfile: + def profile(self) -> Optional[FirefoxProfile]: """:Returns: The Firefox profile to use.""" return self._profile @@ -96,7 +99,9 @@ def profile(self, new_profile: Union[str, FirefoxProfile]) -> None: new_profile = FirefoxProfile(new_profile) self._profile = new_profile - def enable_mobile(self, android_package: str = "org.mozilla.firefox", android_activity=None, device_serial=None): + def enable_mobile( + self, android_package: Optional[str] = "org.mozilla.firefox", android_activity=None, device_serial=None + ): super().enable_mobile(android_package, android_activity, device_serial) def to_capabilities(self) -> dict: @@ -106,7 +111,7 @@ def to_capabilities(self) -> dict: # it will defer to geckodriver to find the system Firefox # and generate a fresh profile. caps = self._caps - opts = {} + opts: Dict[str, Any] = {} if self._binary_location: opts["binary"] = self._binary_location diff --git a/py/selenium/webdriver/wpewebkit/service.py b/py/selenium/webdriver/wpewebkit/service.py index 26e8747bae803..bd90f51daf61b 100644 --- a/py/selenium/webdriver/wpewebkit/service.py +++ b/py/selenium/webdriver/wpewebkit/service.py @@ -49,7 +49,7 @@ def __init__( log_output=log_output, env=env, **kwargs, - ) # type: ignore + ) def command_line_args(self) -> typing.List[str]: return ["-p", f"{self.port}"] + self.service_args